mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-21 09:08:53 +01:00
Move md workspace symbol search to language service (#154874)
* Move md workspace symbol search to language service Also implements more of IWorkspace for the server * Revert extra change
This commit is contained in:
@@ -3,37 +3,15 @@
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { Connection, Emitter, Event, InitializeParams, InitializeResult, RequestType, TextDocuments } from 'vscode-languageserver';
|
||||
import { Connection, InitializeParams, InitializeResult, TextDocuments } from 'vscode-languageserver';
|
||||
import { TextDocument } from 'vscode-languageserver-textdocument';
|
||||
import * as lsp from 'vscode-languageserver-types';
|
||||
import * as md from 'vscode-markdown-languageservice';
|
||||
import { URI } from 'vscode-uri';
|
||||
import { LogFunctionLogger } from './logging';
|
||||
import { parseRequestType } from './protocol';
|
||||
import { VsCodeClientWorkspace } from './workspace';
|
||||
|
||||
|
||||
const parseRequestType: RequestType<{ uri: string }, md.Token[], any> = new RequestType('markdown/parse');
|
||||
|
||||
class TextDocumentToITextDocumentAdapter implements md.ITextDocument {
|
||||
public readonly uri: md.IUri;
|
||||
|
||||
public get version(): number { return this._doc.version; }
|
||||
|
||||
public get lineCount(): number { return this._doc.lineCount; }
|
||||
|
||||
constructor(
|
||||
private readonly _doc: TextDocument,
|
||||
) {
|
||||
this.uri = URI.parse(this._doc.uri);
|
||||
}
|
||||
|
||||
getText(range?: md.IRange | undefined): string {
|
||||
return this._doc.getText(range);
|
||||
}
|
||||
|
||||
positionAt(offset: number): md.IPosition {
|
||||
return this._doc.positionAt(offset);
|
||||
}
|
||||
}
|
||||
declare const TextDecoder: any;
|
||||
|
||||
export function startServer(connection: Connection) {
|
||||
const documents = new TextDocuments(TextDocument);
|
||||
@@ -45,11 +23,11 @@ export function startServer(connection: Connection) {
|
||||
documentSymbolProvider: true,
|
||||
foldingRangeProvider: true,
|
||||
selectionRangeProvider: true,
|
||||
workspaceSymbolProvider: true,
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
const parser = new class implements md.IMdParser {
|
||||
slugifier = md.githubSlugifier;
|
||||
|
||||
@@ -58,42 +36,15 @@ export function startServer(connection: Connection) {
|
||||
}
|
||||
};
|
||||
|
||||
const workspace = new class implements md.IMdWorkspace {
|
||||
|
||||
private readonly _onDidChangeMarkdownDocument = new Emitter<md.ITextDocument>();
|
||||
onDidChangeMarkdownDocument: Event<md.ITextDocument> = this._onDidChangeMarkdownDocument.event;
|
||||
|
||||
private readonly _onDidCreateMarkdownDocument = new Emitter<md.ITextDocument>();
|
||||
onDidCreateMarkdownDocument: Event<md.ITextDocument> = this._onDidCreateMarkdownDocument.event;
|
||||
|
||||
private readonly _onDidDeleteMarkdownDocument = new Emitter<md.IUri>();
|
||||
onDidDeleteMarkdownDocument: Event<md.IUri> = this._onDidDeleteMarkdownDocument.event;
|
||||
|
||||
async getAllMarkdownDocuments(): Promise<Iterable<md.ITextDocument>> {
|
||||
return documents.all().map(doc => new TextDocumentToITextDocumentAdapter(doc));
|
||||
}
|
||||
hasMarkdownDocument(resource: md.IUri): boolean {
|
||||
return !!documents.get(resource.toString());
|
||||
}
|
||||
async getOrLoadMarkdownDocument(_resource: md.IUri): Promise<md.ITextDocument | undefined> {
|
||||
return undefined;
|
||||
}
|
||||
async pathExists(_resource: md.IUri): Promise<boolean> {
|
||||
return false;
|
||||
}
|
||||
async readDirectory(_resource: md.IUri): Promise<[string, { isDir: boolean }][]> {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const workspace = new VsCodeClientWorkspace(connection, documents);
|
||||
const logger = new LogFunctionLogger(connection.console.log.bind(connection.console));
|
||||
const provider = md.createLanguageService(workspace, parser, logger);
|
||||
const provider = md.createLanguageService({ workspace, parser, logger });
|
||||
|
||||
connection.onDocumentSymbol(async (params, token): Promise<lsp.DocumentSymbol[]> => {
|
||||
try {
|
||||
const document = documents.get(params.textDocument.uri);
|
||||
if (document) {
|
||||
return await provider.provideDocumentSymbols(new TextDocumentToITextDocumentAdapter(document), token);
|
||||
return await provider.provideDocumentSymbols(document, token);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e.stack);
|
||||
@@ -105,7 +56,7 @@ export function startServer(connection: Connection) {
|
||||
try {
|
||||
const document = documents.get(params.textDocument.uri);
|
||||
if (document) {
|
||||
return await provider.provideFoldingRanges(new TextDocumentToITextDocumentAdapter(document), token);
|
||||
return await provider.provideFoldingRanges(document, token);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e.stack);
|
||||
@@ -117,7 +68,7 @@ export function startServer(connection: Connection) {
|
||||
try {
|
||||
const document = documents.get(params.textDocument.uri);
|
||||
if (document) {
|
||||
return await provider.provideSelectionRanges(new TextDocumentToITextDocumentAdapter(document), params.positions, token);
|
||||
return await provider.provideSelectionRanges(document, params.positions, token);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e.stack);
|
||||
@@ -125,5 +76,15 @@ export function startServer(connection: Connection) {
|
||||
return [];
|
||||
});
|
||||
|
||||
connection.onWorkspaceSymbol(async (params, token): Promise<lsp.WorkspaceSymbol[]> => {
|
||||
try {
|
||||
return await provider.provideWorkspaceSymbols(params.query, token);
|
||||
} catch (e) {
|
||||
console.error(e.stack);
|
||||
}
|
||||
return [];
|
||||
});
|
||||
|
||||
connection.listen();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user