From 3f77cd32da7e156c3491061f282a5eddb55b8128 Mon Sep 17 00:00:00 2001 From: zhanba Date: Tue, 30 Jan 2024 17:36:17 +0800 Subject: [PATCH] fix: fix type errors --- .../src/common/codeConverter.ts | 3 +- .../src/common/documentSymbol.ts | 20 ++++---- .../src/common/protocolConverter.ts | 22 ++++---- .../src/common/semanticTokens.ts | 14 ++--- .../src/common/vscodeAdaptor/convertor.ts | 14 +++-- .../vscodeAdaptor/diagnosticCollection.ts | 3 +- .../src/common/vscodeAdaptor/extHostTypes.ts | 30 ++++++++--- .../src/common/vscodeAdaptor/fileWatcher.ts | 2 - .../src/common/vscodeAdaptor/libro-fs.ts | 51 +++++++++++++++++++ .../common/vscodeAdaptor/libroWorkspace.ts | 9 ++-- .../common/vscodeAdaptor/monaco-converter.ts | 19 ++++--- .../common/vscodeAdaptor/monacoLanguages.ts | 8 +-- 12 files changed, 135 insertions(+), 60 deletions(-) create mode 100644 packages/libro-language-client/src/common/vscodeAdaptor/libro-fs.ts diff --git a/packages/libro-language-client/src/common/codeConverter.ts b/packages/libro-language-client/src/common/codeConverter.ts index 0e14c00dd..7ded1b3c7 100644 --- a/packages/libro-language-client/src/common/codeConverter.ts +++ b/packages/libro-language-client/src/common/codeConverter.ts @@ -45,6 +45,7 @@ import type { TextDocumentWillSaveEvent, TextEdit, Uri, + SymbolInformation as VSymbolInformation, } from 'vscode'; import ProtocolCallHierarchyItem from './protocolCallHierarchyItem.js'; @@ -237,7 +238,7 @@ export interface Converter { asTypeHierarchyItem(value: TypeHierarchyItem): proto.TypeHierarchyItem; - asWorkspaceSymbol(item: SymbolInformation): proto.WorkspaceSymbol; + asWorkspaceSymbol(item: VSymbolInformation): proto.WorkspaceSymbol; asInlayHint(value: InlayHint): proto.InlayHint; diff --git a/packages/libro-language-client/src/common/documentSymbol.ts b/packages/libro-language-client/src/common/documentSymbol.ts index 246ea1e78..a95281310 100644 --- a/packages/libro-language-client/src/common/documentSymbol.ts +++ b/packages/libro-language-client/src/common/documentSymbol.ts @@ -3,16 +3,6 @@ * Licensed under the MIT License. See License.txt in the project root for license information. * ------------------------------------------------------------------------------------------ */ -import type { - TextDocument, - Disposable, - CancellationToken, - ProviderResult, - DocumentSymbolProvider, - DocumentSymbolProviderMetadata, - SymbolInformation as VSymbolInformation, - DocumentSymbol as VDocumentSymbol, -} from 'vscode'; import type { ClientCapabilities, DocumentSelector, @@ -27,6 +17,16 @@ import { SymbolKind, SymbolTag, } from '@difizen/vscode-languageserver-protocol'; +import type { + TextDocument, + Disposable, + CancellationToken, + ProviderResult, + DocumentSymbolProvider, + DocumentSymbolProviderMetadata, + SymbolInformation as VSymbolInformation, + DocumentSymbol as VDocumentSymbol, +} from 'vscode'; import type { FeatureClient } from './features.js'; import { ensure, TextDocumentLanguageFeature } from './features.js'; diff --git a/packages/libro-language-client/src/common/protocolConverter.ts b/packages/libro-language-client/src/common/protocolConverter.ts index bafdce1df..7ea8ab73b 100644 --- a/packages/libro-language-client/src/common/protocolConverter.ts +++ b/packages/libro-language-client/src/common/protocolConverter.ts @@ -32,6 +32,8 @@ import type { TypeHierarchyItem, WorkspaceEditEntryMetadata, TextDocument, + MarkdownString as VMarkdownString, + SemanticTokens as VSemanticTokens, } from 'vscode'; import { URI } from 'vscode-uri'; @@ -72,14 +74,14 @@ import { FoldingRange, FoldingRangeKind, SelectionRange, - SemanticTokens, SemanticTokensEdit, SemanticTokensEdits, CallHierarchyOutgoingCall, InlineSuggestion as InlineCompletionItem, InlineSuggestionList as InlineCompletionList, - LinkedEditingRanges, MarkdownString, + SemanticTokens, + LinkedEditingRanges, } from './vscodeAdaptor/vscodeAdaptor.js'; import { DiagnosticRelatedInformation, @@ -479,11 +481,11 @@ export interface Converter { asSemanticTokens( value: ls.SemanticTokens, token?: CancellationToken, - ): Promise; + ): Promise; asSemanticTokens( value: ls.SemanticTokens | undefined | null, token?: CancellationToken, - ): Promise; + ): Promise; asSemanticTokensEdit(value: ls.SemanticTokensEdit): SemanticTokensEdit; @@ -821,14 +823,14 @@ export function createConverter( function asHoverContent( value: ls.MarkedString | ls.MarkedString[] | ls.MarkupContent, - ): MarkdownString | MarkdownString[] { + ): VMarkdownString | VMarkdownString[] { if (Is.string(value)) { return asMarkdownString(value); } else if (CodeBlock.is(value)) { const result = asMarkdownString(); return result.appendCodeblock(value.value, value.language); } else if (Array.isArray(value)) { - const result: MarkdownString[] = []; + const result: VMarkdownString[] = []; for (const element of value) { const item = asMarkdownString(); if (CodeBlock.is(element)) { @@ -844,7 +846,7 @@ export function createConverter( } } - function asDocumentation(value: string | ls.MarkupContent): string | MarkdownString { + function asDocumentation(value: string | ls.MarkupContent): string | VMarkdownString { if (Is.string(value)) { return value; } else { @@ -859,7 +861,7 @@ export function createConverter( } } - function asMarkdownString(value?: string | ls.MarkupContent): MarkdownString { + function asMarkdownString(value?: string | ls.MarkupContent): VMarkdownString { let result: MarkdownString; if (value === undefined || typeof value === 'string') { result = new MarkdownString(value); @@ -2156,7 +2158,7 @@ export function createConverter( return result; } - function asTooltip(value: string | ls.MarkupContent): string | MarkdownString { + function asTooltip(value: string | ls.MarkupContent): string | VMarkdownString { if (typeof value === 'string') { return value; } @@ -2514,7 +2516,7 @@ export function createConverter( if (typeof item.insertText === 'string') { insertText = item.insertText; } else { - insertText = new SnippetString(item.insertText.value); + insertText = new SnippetString(item.insertText.value).value; } let command: Command | undefined = undefined; diff --git a/packages/libro-language-client/src/common/semanticTokens.ts b/packages/libro-language-client/src/common/semanticTokens.ts index a39dcc746..75e66380d 100644 --- a/packages/libro-language-client/src/common/semanticTokens.ts +++ b/packages/libro-language-client/src/common/semanticTokens.ts @@ -25,6 +25,7 @@ import { SemanticTokensRegistrationType, } from '@difizen/vscode-languageserver-protocol'; import type { + SemanticTokens as VSemanticTokens, CancellationToken, DocumentRangeSemanticTokensProvider, DocumentSemanticTokensProvider, @@ -32,6 +33,7 @@ import type { SemanticTokensEdits, TextDocument, SemanticTokensLegend, + Range, } from 'vscode'; import type { FeatureClient } from './features.js'; @@ -44,7 +46,7 @@ export interface DocumentSemanticsTokensSignature { this: void, document: TextDocument, token: CancellationToken, - ): ProviderResult; + ): ProviderResult; } export interface DocumentSemanticsTokensEditsSignature { @@ -53,7 +55,7 @@ export interface DocumentSemanticsTokensEditsSignature { document: TextDocument, previousResultId: string, token: CancellationToken, - ): ProviderResult; + ): ProviderResult; } export interface DocumentRangeSemanticTokensSignature { @@ -62,7 +64,7 @@ export interface DocumentRangeSemanticTokensSignature { document: TextDocument, range: Range, token: CancellationToken, - ): ProviderResult; + ): ProviderResult; } /** @@ -76,21 +78,21 @@ export interface SemanticTokensMiddleware { document: TextDocument, token: CancellationToken, next: DocumentSemanticsTokensSignature, - ) => ProviderResult; + ) => ProviderResult; provideDocumentSemanticTokensEdits?: ( this: void, document: TextDocument, previousResultId: string, token: CancellationToken, next: DocumentSemanticsTokensEditsSignature, - ) => ProviderResult; + ) => ProviderResult; provideDocumentRangeSemanticTokens?: ( this: void, document: TextDocument, range: Range, token: CancellationToken, next: DocumentRangeSemanticTokensSignature, - ) => ProviderResult; + ) => ProviderResult; } export interface SemanticTokensProviderShape { diff --git a/packages/libro-language-client/src/common/vscodeAdaptor/convertor.ts b/packages/libro-language-client/src/common/vscodeAdaptor/convertor.ts index 9fc051012..32333bc08 100644 --- a/packages/libro-language-client/src/common/vscodeAdaptor/convertor.ts +++ b/packages/libro-language-client/src/common/vscodeAdaptor/convertor.ts @@ -8,7 +8,10 @@ import { EndOfLine, NotebookCellKind, Uri } from './vscodeAdaptor.js'; export const l2c = { asNotebookDocument(libroView: LibroView): NotebookDocument { - const model = libroView.model; + const model = libroView.model as any; + if (!model.filePath) { + throw new Error('no filePath: invalid libro jupyter model'); + } const filePath = model.filePath as string; return { uri: Uri.parse(filePath), @@ -28,17 +31,22 @@ export const l2c = { }, asNotebookCell(cell: CellView): NotebookCell { + const model = cell.parent.model as any; + if (!model.filePath) { + throw new Error('no filePath: invalid libro jupyter model'); + } + const filePath = model.filePath as string; return { index: cell.parent.findCellIndex(cell), notebook: l2c.asNotebookDocument(cell.parent), kind: cell.model.type === 'code' ? NotebookCellKind.Code : NotebookCellKind.Markup, document: { - uri: Uri.parse(cell.parent.model.filePath).with({ + uri: Uri.parse(filePath).with({ scheme: NotebookDocumentSyncFeature.CellScheme, query: `cellid=${cell.model.id}`, }), - fileName: cell.parent.model.filePath, + fileName: filePath, isUntitled: false, languageId: 'python', version: 0, diff --git a/packages/libro-language-client/src/common/vscodeAdaptor/diagnosticCollection.ts b/packages/libro-language-client/src/common/vscodeAdaptor/diagnosticCollection.ts index 36c9d2927..980ec34bb 100644 --- a/packages/libro-language-client/src/common/vscodeAdaptor/diagnosticCollection.ts +++ b/packages/libro-language-client/src/common/vscodeAdaptor/diagnosticCollection.ts @@ -1,9 +1,8 @@ -import { DisposableCollection } from '@difizen/mana-app'; import { MarkerSeverity } from '@difizen/monaco-editor-core'; import * as monaco from '@difizen/monaco-editor-core'; import type { editor } from '@difizen/monaco-editor-core'; import type { DiagnosticCollection } from 'vscode'; -import type { Diagnostic, Uri, Disposable } from 'vscode'; +import type { Diagnostic, Uri } from 'vscode'; import { URI } from 'vscode-uri'; import * as c2p from '../codeConverter.js'; diff --git a/packages/libro-language-client/src/common/vscodeAdaptor/extHostTypes.ts b/packages/libro-language-client/src/common/vscodeAdaptor/extHostTypes.ts index 082a1ff46..5f07fda17 100644 --- a/packages/libro-language-client/src/common/vscodeAdaptor/extHostTypes.ts +++ b/packages/libro-language-client/src/common/vscodeAdaptor/extHostTypes.ts @@ -1248,7 +1248,7 @@ export class Diagnostic { message: string; severity: DiagnosticSeverity; source?: string; - code?: string | number; + code?: string | number | { value: string | number; target: URI }; relatedInformation?: DiagnosticRelatedInformation[]; tags?: DiagnosticTag[]; @@ -1409,7 +1409,7 @@ export class SymbolInformation { location!: Location; kind: SymbolKind; tags?: SymbolTag[]; - containerName: string | undefined; + containerName: string; constructor( name: string, @@ -1433,7 +1433,7 @@ export class SymbolInformation { ) { this.name = name; this.kind = kind; - this.containerName = containerName; + this.containerName = containerName ?? ''; if (typeof rangeOrContainer === 'string') { this.containerName = rangeOrContainer; @@ -1507,12 +1507,14 @@ export class CodeAction { edit?: WorkspaceEdit; - diagnostics?: Diagnostic[]; + diagnostics?: vscode.Diagnostic[]; kind?: CodeActionKind; isPreferred?: boolean; + disabled?: { reason: string }; + constructor(title: string, kind?: CodeActionKind) { this.title = title; this.kind = kind; @@ -4231,10 +4233,22 @@ export enum StandardTokenType { } export class LinkedEditingRanges { - constructor( - public readonly ranges: Range[], - public readonly wordPattern: RegExp, - ) {} + constructor(ranges: Range[], wordPattern?: RegExp | undefined) { + this.ranges = ranges; + this.wordPattern = wordPattern; + } + + /** + * A list of ranges that can be edited together. The ranges must have + * identical length and text content. The ranges cannot overlap. + */ + readonly ranges: Range[]; + + /** + * An optional word pattern that describes valid contents for the given ranges. + * If no pattern is provided, the language configuration's word pattern will be used. + */ + readonly wordPattern: RegExp | undefined; } //#region ports diff --git a/packages/libro-language-client/src/common/vscodeAdaptor/fileWatcher.ts b/packages/libro-language-client/src/common/vscodeAdaptor/fileWatcher.ts index c283d4e9c..4fa40e68e 100644 --- a/packages/libro-language-client/src/common/vscodeAdaptor/fileWatcher.ts +++ b/packages/libro-language-client/src/common/vscodeAdaptor/fileWatcher.ts @@ -1,5 +1,3 @@ -import type { NotebookView } from '@difizen/libro-core'; -import { EditorCellView } from '@difizen/libro-core'; import { LibroService } from '@difizen/libro-core'; import { inject, singleton } from '@difizen/mana-app'; import type { Event, FileSystemWatcher, GlobPattern, Uri } from 'vscode'; diff --git a/packages/libro-language-client/src/common/vscodeAdaptor/libro-fs.ts b/packages/libro-language-client/src/common/vscodeAdaptor/libro-fs.ts new file mode 100644 index 000000000..58482e014 --- /dev/null +++ b/packages/libro-language-client/src/common/vscodeAdaptor/libro-fs.ts @@ -0,0 +1,51 @@ +import type { FileStat, FileSystem, Uri } from 'vscode'; + +import { FileType } from './vscodeAdaptor.js'; + +export class LibroFS implements FileSystem { + stat(uri: Uri): Thenable { + return Promise.resolve({ + type: FileType.File, + ctime: 0, + mtime: 0, + size: 0, + }); + } + readDirectory(uri: Uri): Thenable<[string, FileType][]> { + throw new Error('Method not implemented.'); + } + createDirectory(uri: Uri): Thenable { + throw new Error('Method not implemented.'); + } + readFile(uri: Uri): Thenable { + throw new Error('Method not implemented.'); + } + writeFile(uri: Uri, content: Uint8Array): Thenable { + throw new Error('Method not implemented.'); + } + delete( + uri: Uri, + options?: + | { recursive?: boolean | undefined; useTrash?: boolean | undefined } + | undefined, + ): Thenable { + throw new Error('Method not implemented.'); + } + rename( + source: Uri, + target: Uri, + options?: { overwrite?: boolean | undefined } | undefined, + ): Thenable { + throw new Error('Method not implemented.'); + } + copy( + source: Uri, + target: Uri, + options?: { overwrite?: boolean | undefined } | undefined, + ): Thenable { + throw new Error('Method not implemented.'); + } + isWritableFileSystem(scheme: string): boolean | undefined { + throw new Error('Method not implemented.'); + } +} diff --git a/packages/libro-language-client/src/common/vscodeAdaptor/libroWorkspace.ts b/packages/libro-language-client/src/common/vscodeAdaptor/libroWorkspace.ts index 19e0c6ea2..3fdd64095 100644 --- a/packages/libro-language-client/src/common/vscodeAdaptor/libroWorkspace.ts +++ b/packages/libro-language-client/src/common/vscodeAdaptor/libroWorkspace.ts @@ -1,6 +1,5 @@ -import type { NotebookView } from '@difizen/libro-core'; -import { EditorCellView, LibroService } from '@difizen/libro-core'; -import { Disposable, inject, noop, singleton } from '@difizen/mana-app'; +import { LibroService } from '@difizen/libro-core'; +import { inject, noop, singleton } from '@difizen/mana-app'; import type { NotebookDocument, WorkspaceFolder, @@ -24,11 +23,13 @@ import type { FileSystemWatcher, Uri, TextDocument, + FileSystem, } from 'vscode'; import { l2c } from './convertor.js'; import { Range } from './extHostTypes.js'; import { LibroFileWatcher } from './fileWatcher.js'; +import { LibroFS } from './libro-fs.js'; import { ILibroWorkspace } from './services.js'; import { unsupported } from './util.js'; @@ -46,7 +47,7 @@ export class LibroWorkspace implements ILibroWorkspace { ); } textDocuments: TextDocument[] = []; - fs: FileSystem; + fs: FileSystem = new LibroFS(); onDidChangeWorkspaceFolders: Event = () => { return { dispose: () => { diff --git a/packages/libro-language-client/src/common/vscodeAdaptor/monaco-converter.ts b/packages/libro-language-client/src/common/vscodeAdaptor/monaco-converter.ts index 30e518a8a..c77e7dfd5 100644 --- a/packages/libro-language-client/src/common/vscodeAdaptor/monaco-converter.ts +++ b/packages/libro-language-client/src/common/vscodeAdaptor/monaco-converter.ts @@ -593,11 +593,10 @@ export class MonacoToProtocolConverter { if (context === void 0 || context === null) { return context; } - // FIXME: CodeActionTriggerKind is missing return CodeActionContext.create( diagnostics, Is.isString(context.only) ? [context.only] : undefined, - undefined, + context.trigger, ); } @@ -781,11 +780,11 @@ export class ProtocolToMonacoConverter { annotation: ls.ChangeAnnotationIdentifier | undefined, ) => monaco.languages.WorkspaceEditMetadata | undefined, modelVersionId?: number, - ): monaco.languages.WorkspaceTextEdit[] { + ): monaco.languages.IWorkspaceTextEdit[] { return edits.map((edit) => ({ resource: resource, - edit: this.asTextEdit(edit), - modelVersionId, + textEdit: this.asTextEdit(edit), + versionId: modelVersionId, metadata: AnnotatedTextEdit.is(edit) ? asMetadata(edit.annotationId) : undefined, })); } @@ -829,26 +828,26 @@ export class ProtocolToMonacoConverter { } }; const edits: ( - | monaco.languages.WorkspaceTextEdit - | monaco.languages.WorkspaceFileEdit + | monaco.languages.IWorkspaceTextEdit + | monaco.languages.IWorkspaceFileEdit )[] = []; if (item.documentChanges) { item.documentChanges.forEach((change) => { if (ls.CreateFile.is(change)) { - edits.push({ + edits.push({ newUri: this._monaco.Uri.parse(change.uri), options: change.options, metadata: asMetadata(change.annotationId), }); } else if (ls.RenameFile.is(change)) { - edits.push({ + edits.push({ oldUri: this._monaco.Uri.parse(change.oldUri), newUri: this._monaco.Uri.parse(change.newUri), options: change.options, metadata: asMetadata(change.annotationId), }); } else if (ls.DeleteFile.is(change)) { - edits.push({ + edits.push({ oldUri: this._monaco.Uri.parse(change.uri), options: change.options, metadata: asMetadata(change.annotationId), diff --git a/packages/libro-language-client/src/common/vscodeAdaptor/monacoLanguages.ts b/packages/libro-language-client/src/common/vscodeAdaptor/monacoLanguages.ts index 42f2433c7..4704d7f9d 100644 --- a/packages/libro-language-client/src/common/vscodeAdaptor/monacoLanguages.ts +++ b/packages/libro-language-client/src/common/vscodeAdaptor/monacoLanguages.ts @@ -1,6 +1,6 @@ import { singleton } from '@difizen/mana-app'; import * as monaco from '@difizen/monaco-editor-core'; -import { score } from '@difizen/monaco-editor-core/esm/vs/editor/common/languageSelector.js'; +// import { score } from '@difizen/monaco-editor-core/esm/vs/editor/common/languageSelector.js'; import type { DiagnosticCollection, TextDocument, @@ -52,7 +52,7 @@ import { ProtocolToMonacoConverter, } from './monaco-converter.js'; import { IMonacoLanguages } from './services.js'; -import { CancellationToken, CompletionTriggerKind } from './vscodeAdaptor.js'; +import { CompletionTriggerKind } from './vscodeAdaptor.js'; function overrideWithResolvedValue(item: T, resolvedItem: T): void { for (const key in resolvedItem) { @@ -180,7 +180,7 @@ export class MonacoLanguages implements IMonacoLanguages { return { contents: hover?.contents, range: this.p2m.asRange(this.c2p.asRange(hover.range)), - }; + } as monaco.languages.Hover; }, }; } @@ -371,7 +371,7 @@ export class MonacoLanguages implements IMonacoLanguages { const result = await provider?.provideCompletionItems( this.p2c.asTextDcouemnt(params.textDocument), this.p2c.asPosition(params.position), - CancellationToken, + token, { triggerCharacter: params.context?.triggerCharacter ?? '.', triggerKind: