Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add libro package #366

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
"packageManager": "[email protected]",
"dependencies": {
"@ant-design/icons": "^5.4.0",
"@difizen/libro-core": "^0.3.4",
"@difizen/libro-jupyter": "^0.3.4",
"@difizen/libro-lab": "^0.3.4",
"@difizen/libro-terminal": "^0.3.4",
"@difizen/libro": "latest",
"@difizen/libro-core": "latest",
"@difizen/libro-jupyter": "latest",
"@difizen/libro-lab": "latest",
"@difizen/libro-terminal": "latest",
"@difizen/mana-app": "latest",
"@difizen/mana-l10n": "latest",
"@difizen/mana-react": "latest",
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/lab/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ServerConnection, ServerManager } from '@difizen/libro-lab';
import { ServerConnection, ServerManager } from '@difizen/libro';
import { ApplicationContribution, inject, singleton } from '@difizen/mana-app';

@singleton({ contrib: ApplicationContribution })
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/lab/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LibroLabModule } from '@difizen/libro-lab';
import { LibroLabModule } from '@difizen/libro';
import { ManaAppPreset, ManaComponents, ManaModule } from '@difizen/mana-app';

import { LibroApp } from './app.js';
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/output/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LibroService } from '@difizen/libro-core';
import { LibroService } from '@difizen/libro';
import { RootSlotId } from '@difizen/mana-app';
import { ApplicationContribution, SlotViewManager } from '@difizen/mana-app';
import { inject, singleton } from '@difizen/mana-app';
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/output/content.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { INotebookContent } from '@difizen/libro-jupyter';
import { ContentContribution } from '@difizen/libro-jupyter';
import type { INotebookContent } from '@difizen/libro';
import { ContentContribution } from '@difizen/libro';
import { singleton } from '@difizen/mana-app';

import content from './OutputExamples.ipynb.json';
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/output/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LibroJupyterModule } from '@difizen/libro-jupyter';
import { LibroJupyterModule } from '@difizen/libro';
import { ManaAppPreset, ManaComponents, ManaModule } from '@difizen/mana-app';

import { LibroApp } from './app.js';
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/terminal/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ServerConnection, ServerManager } from '@difizen/libro-jupyter';
import { ServerConnection, ServerManager } from '@difizen/libro';
import { ConfigurationService } from '@difizen/mana-app';
import { SlotViewManager } from '@difizen/mana-app';
import { ApplicationContribution, ViewManager } from '@difizen/mana-app';
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/workbench/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ServerConnection,
LibroJupyterConfiguration,
ServerManager,
} from '@difizen/libro-jupyter';
} from '@difizen/libro';
import type { FileTreeView } from '@difizen/mana-app';
import { ConfigurationService } from '@difizen/mana-app';
import { FileTreeViewFactory, SlotViewManager } from '@difizen/mana-app';
Expand Down
3 changes: 1 addition & 2 deletions apps/docs/src/workbench/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LibroJupyterModule } from '@difizen/libro-jupyter';
import { LibroJupyterModule } from '@difizen/libro';
import {
ManaAppPreset,
ManaComponents,
Expand All @@ -7,7 +7,6 @@ import {
RootSlotId,
CardTabView,
FileTreeView,
createViewPreference,
} from '@difizen/mana-app';

import { LibroApp } from './app.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type {
InlineCompletionProvider,
CancellationToken,
EditorCancellationToken,
IIntelligentCompletionsResult,
ICompletionContext,
} from '@difizen/libro-code-editor';
import { singleton } from '@difizen/mana-app';

import { CompletionRequest } from './inline-completion-request.js';
import { AiCompletionRequest } from './inline-completion-request.js';
import { raceCancellation, sleep } from './utils.js';

// 缓存最近一次的补全结果
Expand All @@ -21,11 +21,11 @@ const inlineCompletionCache: {
};

class ReqStack {
queue: CompletionRequest[];
queue: AiCompletionRequest[];
constructor() {
this.queue = [];
}
addReq(reqRequest: CompletionRequest) {
addReq(reqRequest: AiCompletionRequest) {
this.queue.push(reqRequest);
}
runReq() {
Expand Down Expand Up @@ -66,12 +66,12 @@ export class AICompletionProvider implements InlineCompletionProvider {

async provideInlineCompletionItems(
context: ICompletionContext,
token: CancellationToken,
token: EditorCancellationToken,
) {
this.cancelRequest();

// 放入队列
const requestImp = new CompletionRequest(context, token);
const requestImp = new AiCompletionRequest(context, token);
this.reqStack.addReq(requestImp);

await raceCancellation(sleep(this.inlineComletionsDebounceTime), token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import type {
IAICompletionOption,
ICompletionContext,
IIntelligentCompletionsResult,
CancellationToken,
EditorCancellationToken,
} from '@difizen/libro-code-editor';
import { transient } from '@difizen/mana-app';
import { v4 } from 'uuid';

import { generateInstructionsPrompt } from './Prompt/instruction.js';

@transient()
export class CompletionRequest {
export class AiCompletionRequest {
isCancelFlag: boolean;
id: string;

constructor(
public context: ICompletionContext,
public token: CancellationToken,
public token: EditorCancellationToken,
) {
this.isCancelFlag = false;
this.id = v4();
Expand All @@ -25,7 +25,7 @@ export class CompletionRequest {
// 拼接上下文信息
protected constructRequestContext(
context: ICompletionContext,
token: CancellationToken,
token: EditorCancellationToken,
): IAICompletionOption {
// const prompt = lineBasedPromptProcessor.processPrefix(context.prefix);
// const suffix = lineBasedPromptProcessor.processSuffix(context.suffix);
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-ai-native/src/ai-inline-completions/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ManaModule } from '@difizen/mana-app';

import { AICompletionContribution } from './inline-completion-contribution.js';
import { AICompletionProvider } from './inline-completion-provider.js';
import { CompletionRequest } from './inline-completion-request.js';
import { AiCompletionRequest } from './inline-completion-request.js';

export const LibroAICompletionModule = ManaModule.create()
.register(CompletionRequest, AICompletionProvider, AICompletionContribution)
.register(AiCompletionRequest, AICompletionProvider, AICompletionContribution)
.dependOn(CodeEditorModule);
8 changes: 4 additions & 4 deletions packages/libro-ai-native/src/ai-inline-completions/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CancellationToken } from '@difizen/libro-code-editor';
import type { EditorCancellationToken } from '@difizen/libro-code-editor';

import { lineBasedCompletionModelConfigs } from './contant.js';

Expand Down Expand Up @@ -39,16 +39,16 @@ export function sleep(time: number) {

export function raceCancellation<T>(
promise: Promise<T>,
token: CancellationToken,
token: EditorCancellationToken,
): Promise<T | undefined>;
export function raceCancellation<T>(
promise: Promise<T>,
token: CancellationToken,
token: EditorCancellationToken,
defaultValue: T,
): Promise<T>;
export function raceCancellation<T>(
promise: Promise<T>,
token: CancellationToken,
token: EditorCancellationToken,
defaultValue?: T,
): Promise<T | undefined> {
return Promise.race([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export interface InlineCompletionProvider {
provideInlineCompletionItems: ProvideInlineCompletionsFunction;
}

export interface CancellationToken {
export interface EditorCancellationToken {
/**
* A flag signalling is cancellation has been requested.
*/
Expand Down Expand Up @@ -169,5 +169,5 @@ export interface InlineCompletionImplement {

export type ProvideInlineCompletionsFunction = (
context: ICompletionContext,
token: CancellationToken,
token: EditorCancellationToken,
) => Promise<IIntelligentCompletionsResult | undefined>;
2 changes: 1 addition & 1 deletion packages/libro-codemirror/src/auto-complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

### Sources

@CompletionContext
@CodemirrorCompletionContext

@CompletionResult

Expand Down
14 changes: 7 additions & 7 deletions packages/libro-codemirror/src/auto-complete/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface Completion {
}

/// An instance of this is passed to completion source functions.
export class CompletionContext {
export class CodemirrorCompletionContext {
/// @internal
abortListeners: (() => void)[] | null = [];

Expand Down Expand Up @@ -105,7 +105,7 @@ export class CompletionContext {

/// Allows you to register abort handlers, which will be called when
/// the query is
/// [aborted](#autocomplete.CompletionContext.aborted).
/// [aborted](#autocomplete.CodemirrorCompletionContext.aborted).
addEventListener(type: 'abort', listener: () => void) {
if (type === 'abort' && this.abortListeners) {
this.abortListeners.push(listener);
Expand Down Expand Up @@ -144,7 +144,7 @@ export function completeFromList(
const [validFor, match] = options.every((o) => /^\w+$/.test(o.label))
? [/\w*$/, /\w+$/]
: prefixMatch(options);
return (context: CompletionContext) => {
return (context: CodemirrorCompletionContext) => {
const token = context.matchBefore(match);
return token || context.explicit
? { from: token ? token.from : context.pos, options, validFor }
Expand All @@ -158,7 +158,7 @@ export function ifIn(
nodes: readonly string[],
source: CompletionSource,
): CompletionSource {
return (context: CompletionContext) => {
return (context: CodemirrorCompletionContext) => {
for (
let pos: SyntaxNode | null = syntaxTree(context.state).resolveInner(
context.pos,
Expand All @@ -181,7 +181,7 @@ export function ifNotIn(
nodes: readonly string[],
source: CompletionSource,
): CompletionSource {
return (context: CompletionContext) => {
return (context: CodemirrorCompletionContext) => {
for (
let pos: SyntaxNode | null = syntaxTree(context.state).resolveInner(
context.pos,
Expand All @@ -203,7 +203,7 @@ export function ifNotIn(
/// synchronously or as a promise. Returning null indicates no
/// completions are available.
export type CompletionSource = (
context: CompletionContext,
context: CodemirrorCompletionContext,
) => CompletionResult | null | Promise<CompletionResult | null>;

/// Interface for objects returned by completion sources.
Expand Down Expand Up @@ -250,7 +250,7 @@ export interface CompletionResult {
current: CompletionResult,
from: number,
to: number,
context: CompletionContext,
context: CodemirrorCompletionContext,
) => CompletionResult | null;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/libro-codemirror/src/auto-complete/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
cur,
asSource,
ensureAnchor,
CompletionContext,
CodemirrorCompletionContext,
} from './completion.js';
import type { CompletionConfig } from './config.js';
import { completionConfig } from './config.js';
Expand Down Expand Up @@ -389,7 +389,7 @@ export class ActiveResult extends ActiveSource {
this.result,
from,
to,
new CompletionContext(tr.state, pos, explicitPos >= 0),
new CodemirrorCompletionContext(tr.state, pos, explicitPos >= 0),
))
) {
return new ActiveResult(
Expand Down
10 changes: 7 additions & 3 deletions packages/libro-codemirror/src/auto-complete/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
import { ViewPlugin, logException, getTooltip } from '@codemirror/view';

import type { CompletionResult } from './completion.js';
import { cur, CompletionContext, applyCompletion } from './completion.js';
import { cur, CodemirrorCompletionContext, applyCompletion } from './completion.js';
import { completionConfig } from './config.js';
import {
completionState,
Expand Down Expand Up @@ -113,7 +113,7 @@ class RunningQuery {

constructor(
readonly active: ActiveSource,
readonly context: CompletionContext,
readonly context: CodemirrorCompletionContext,
) {}
}

Expand Down Expand Up @@ -216,7 +216,11 @@ export const completionPlugin = ViewPlugin.fromClass(
startQuery(active: ActiveSource) {
const { state } = this.view,
pos = cur(state);
const context = new CompletionContext(state, pos, active.explicitPos === pos);
const context = new CodemirrorCompletionContext(
state,
pos,
active.explicitPos === pos,
);
const pending = new RunningQuery(active, context);
this.running.push(pending);
Promise.resolve(active.source(context))
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-codemirror/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { FoldIcon, UnFoldIcon } from './libro-icon.js';
import { lspPythonCompletion } from './lsp/completion.js';
import { formatKeymap } from './lsp/format.js';
import { lspLint, lspTooltip } from './lsp/index.js';
import { ensure } from './mode.js';
import { codemirrorEnsure } from './mode.js';
import { getTheme, defaultTheme } from './theme.js';
import { tabTooltip, tooltipKeymap } from './tooltip.js';

Expand Down Expand Up @@ -632,7 +632,7 @@ export class EditorConfiguration {
const themeOverload = this.updateThemeOverload(config);
extensions.push(this._themeOverloader.of(themeOverload), insertExt, keymapExt);

ensure(config?.mimetype ?? 'text/x-python')
codemirrorEnsure(config?.mimetype ?? 'text/x-python')
.then((spec) => {
if (spec) {
extensions.push(this.get('language')?.of(spec.support!));
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-codemirror/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { v4 } from 'uuid';
import type { CodeMirrorConfig } from './config.js';
import { EditorConfiguration } from './config.js';
import { stateFactory } from './factory.js';
import { ensure } from './mode.js';
import { codemirrorEnsure } from './mode.js';
import { monitorPlugin } from './monitor.js';

/**
Expand Down Expand Up @@ -922,7 +922,7 @@ export class CodeMirrorEditor implements IEditor {
protected _onMimeTypeChanged(): void {
const mime = this._model.mimeType;
// TODO: should we provide a hook for when the mode is done being set?
void ensure(mime).then((spec) => {
void codemirrorEnsure(mime).then((spec) => {
if (spec) {
this._editorConfig.reconfigureExtension(
this._editor,
Expand Down
9 changes: 5 additions & 4 deletions packages/libro-codemirror/src/lsp/completion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { pythonLanguage } from '@codemirror/lang-python';
import { CompletionItemKind, CompletionTriggerKind } from '@difizen/libro-lsp';
import { LspCompletionTriggerKind } from '@difizen/libro-lsp';
import { LspCompletionItemKind } from '@difizen/libro-lsp';

import type { Completion, CompletionSource } from '../auto-complete/index.js';

Expand All @@ -11,8 +12,8 @@ export type CompletionItemDetailReolve = (
) => Node | null | Promise<Node | null>;

const CompletionItemKindMap = Object.fromEntries(
Object.entries(CompletionItemKind).map(([key, value]) => [value, key]),
) as Record<CompletionItemKind, string>;
Object.entries(LspCompletionItemKind).map(([key, value]) => [value, key]),
) as Record<LspCompletionTriggerKind, string>;

function toSet(chars: Set<string>) {
let preamble = '';
Expand Down Expand Up @@ -86,7 +87,7 @@ export const lspPythonCompletion: CMLSPExtension = ({ lspProvider }) => {
uri: doc.documentInfo.uri,
},
context: {
triggerKind: CompletionTriggerKind.Invoked,
triggerKind: LspCompletionTriggerKind.Invoked,
},
});

Expand Down
Loading
Loading