Skip to content

Commit 2fb56e9

Browse files
mjbvzjoaomoreno
andauthored
Move smart select and folding to md language server (microsoft#154334)
* Move smart select and folding to md language server Also fixes a few minor issues: - Don't log to web console - Remove convert code since it is no longer needed - Use correct extension id * bump cache * Bump package version Co-authored-by: João Moreno <[email protected]>
1 parent f9711df commit 2fb56e9

File tree

10 files changed

+54
-1400
lines changed

10 files changed

+54
-1400
lines changed

build/.cachesalt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2022-06-10T10:20:54.664Z
1+
2022-07-07T05:37:58.279Z

extensions/markdown-language-features/server/src/logging.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@
55

66
import { ILogger } from 'vscode-markdown-languageservice';
77

8-
class ConsoleLogger implements ILogger {
9-
10-
public verbose(title: string, message: string, data?: any): void {
11-
this.appendLine(`[Verbose ${ConsoleLogger.now()}] ${title}: ${message}`);
12-
if (data) {
13-
this.appendLine(ConsoleLogger.data2String(data));
14-
}
15-
}
8+
export class LogFunctionLogger implements ILogger {
169

1710
private static now(): string {
1811
const now = new Date();
@@ -21,10 +14,6 @@ class ConsoleLogger implements ILogger {
2114
+ ':' + String(now.getUTCSeconds()).padStart(2, '0') + '.' + String(now.getMilliseconds()).padStart(3, '0');
2215
}
2316

24-
private appendLine(value: string): void {
25-
console.log(value);
26-
}
27-
2817
private static data2String(data: any): string {
2918
if (data instanceof Error) {
3019
if (typeof data.stack === 'string') {
@@ -37,6 +26,21 @@ class ConsoleLogger implements ILogger {
3726
}
3827
return JSON.stringify(data, undefined, 2);
3928
}
29+
30+
constructor(
31+
private readonly _logFn: typeof console.log
32+
) { }
33+
34+
public verbose(title: string, message: string, data?: any): void {
35+
this.appendLine(`[Verbose ${LogFunctionLogger.now()}] ${title}: ${message}`);
36+
if (data) {
37+
this.appendLine(LogFunctionLogger.data2String(data));
38+
}
39+
}
40+
41+
private appendLine(value: string): void {
42+
this._logFn(value);
43+
}
4044
}
4145

42-
export const consoleLogger = new ConsoleLogger();
46+
export const consoleLogger = new LogFunctionLogger(console.log);

extensions/markdown-language-features/server/src/server.ts

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
import { Connection, Emitter, Event, InitializeParams, InitializeResult, RequestType, TextDocuments } from 'vscode-languageserver';
77
import { TextDocument } from 'vscode-languageserver-textdocument';
8-
import { DocumentSymbol, Position, Range } from 'vscode-languageserver-types';
8+
import * as lsp from 'vscode-languageserver-types';
99
import * as md from 'vscode-markdown-languageservice';
1010
import { URI } from 'vscode-uri';
11-
import { consoleLogger } from './logging';
11+
import { LogFunctionLogger } from './logging';
1212

1313

1414
const parseRequestType: RequestType<{ uri: string }, md.Token[], any> = new RequestType('markdown/parse');
@@ -31,8 +31,7 @@ class TextDocumentToITextDocumentAdapter implements md.ITextDocument {
3131
}
3232

3333
positionAt(offset: number): md.IPosition {
34-
const pos = this._doc.positionAt(offset);
35-
return md.makePosition(pos.line, pos.character);
34+
return this._doc.positionAt(offset);
3635
}
3736
}
3837

@@ -44,6 +43,8 @@ export function startServer(connection: Connection) {
4443
return {
4544
capabilities: {
4645
documentSymbolProvider: true,
46+
foldingRangeProvider: true,
47+
selectionRangeProvider: true,
4748
}
4849
};
4950
});
@@ -85,48 +86,44 @@ export function startServer(connection: Connection) {
8586
}
8687
};
8788

88-
const provider = md.createLanguageService(workspace, parser, consoleLogger);
89+
const logger = new LogFunctionLogger(connection.console.log.bind(connection.console));
90+
const provider = md.createLanguageService(workspace, parser, logger);
8991

90-
connection.onDocumentSymbol(async (documentSymbolParams, _token): Promise<DocumentSymbol[]> => {
92+
connection.onDocumentSymbol(async (params, token): Promise<lsp.DocumentSymbol[]> => {
9193
try {
92-
const document = documents.get(documentSymbolParams.textDocument.uri) as TextDocument | undefined;
94+
const document = documents.get(params.textDocument.uri);
9395
if (document) {
94-
const response = await provider.provideDocumentSymbols(new TextDocumentToITextDocumentAdapter(document));
95-
// TODO: only required because extra methods returned on positions/ranges
96-
return response.map(symbol => convertDocumentSymbol(symbol));
96+
return await provider.provideDocumentSymbols(new TextDocumentToITextDocumentAdapter(document), token);
9797
}
9898
} catch (e) {
9999
console.error(e.stack);
100100
}
101101
return [];
102102
});
103103

104-
connection.listen();
105-
}
106-
107-
108-
function convertDocumentSymbol(sym: DocumentSymbol): DocumentSymbol {
109-
return {
110-
kind: sym.kind,
111-
name: sym.name,
112-
range: convertRange(sym.range),
113-
selectionRange: convertRange(sym.selectionRange),
114-
children: sym.children?.map(convertDocumentSymbol),
115-
detail: sym.detail,
116-
tags: sym.tags,
117-
};
118-
}
104+
connection.onFoldingRanges(async (params, token): Promise<lsp.FoldingRange[]> => {
105+
try {
106+
const document = documents.get(params.textDocument.uri);
107+
if (document) {
108+
return await provider.provideFoldingRanges(new TextDocumentToITextDocumentAdapter(document), token);
109+
}
110+
} catch (e) {
111+
console.error(e.stack);
112+
}
113+
return [];
114+
});
119115

120-
function convertRange(range: Range): Range {
121-
return {
122-
start: convertPosition(range.start),
123-
end: convertPosition(range.end),
124-
};
125-
}
116+
connection.onSelectionRanges(async (params, token): Promise<lsp.SelectionRange[] | undefined> => {
117+
try {
118+
const document = documents.get(params.textDocument.uri);
119+
if (document) {
120+
return await provider.provideSelectionRanges(new TextDocumentToITextDocumentAdapter(document), params.positions, token);
121+
}
122+
} catch (e) {
123+
console.error(e.stack);
124+
}
125+
return [];
126+
});
126127

127-
function convertPosition(start: Position): Position {
128-
return {
129-
character: start.character,
130-
line: start.line,
131-
};
128+
connection.listen();
132129
}

extensions/markdown-language-features/server/yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ vscode-languageserver@^8.0.2-next.4:
4343
vscode-languageserver-protocol "3.17.2-next.6"
4444

4545
vscode-markdown-languageservice@mjbvz/vscode-markdown-languageservice:
46-
version "1.0.0"
47-
resolved "https://codeload.github.com/mjbvz/vscode-markdown-languageservice/tar.gz/e410b5df64659fbc186cf0a7a7c882c451e07b8b"
46+
version "0.0.0-alpha.1"
47+
resolved "https://codeload.github.com/mjbvz/vscode-markdown-languageservice/tar.gz/e1a0e00bf6a99cc543da64964cc0995537647d15"
4848
dependencies:
4949
vscode-languageserver-types "^3.17.1"
5050
vscode-uri "^3.0.3"

extensions/markdown-language-features/src/extension.node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function activate(context: vscode.ExtensionContext) {
3030
}
3131

3232
async function startServer(context: vscode.ExtensionContext, workspace: IMdWorkspace, parser: IMdParser): Promise<void> {
33-
const clientMain = vscode.extensions.getExtension('vscode.css-language-features')?.packageJSON?.main || '';
33+
const clientMain = vscode.extensions.getExtension('vscode.markdown-language-features')?.packageJSON?.main || '';
3434

3535
const serverMain = `./server/${clientMain.indexOf('/dist/') !== -1 ? 'dist' : 'out'}/node/main`;
3636
const serverModule = context.asAbsolutePath(serverMain);

extensions/markdown-language-features/src/extension.shared.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ import { MdLinkProvider, registerDocumentLinkSupport } from './languageFeatures/
1313
import { MdDocumentSymbolProvider } from './languageFeatures/documentSymbols';
1414
import { registerDropIntoEditorSupport } from './languageFeatures/dropIntoEditor';
1515
import { registerFindFileReferenceSupport } from './languageFeatures/fileReferences';
16-
import { registerFoldingSupport } from './languageFeatures/folding';
1716
import { registerPathCompletionSupport } from './languageFeatures/pathCompletions';
1817
import { MdReferencesProvider, registerReferencesSupport } from './languageFeatures/references';
1918
import { registerRenameSupport } from './languageFeatures/rename';
20-
import { registerSmartSelectSupport } from './languageFeatures/smartSelect';
2119
import { registerWorkspaceSymbolSupport } from './languageFeatures/workspaceSymbols';
2220
import { ILogger } from './logging';
2321
import { IMdParser, MarkdownItEngine, MdParsingProvider } from './markdownEngine';
@@ -81,12 +79,10 @@ function registerMarkdownLanguageFeatures(
8179
registerDocumentLinkSupport(selector, linkProvider),
8280
registerDropIntoEditorSupport(selector),
8381
registerFindFileReferenceSupport(commandManager, referencesProvider),
84-
registerFoldingSupport(selector, parser, tocProvider),
8582
registerPasteSupport(selector),
8683
registerPathCompletionSupport(selector, workspace, parser, linkProvider),
8784
registerReferencesSupport(selector, referencesProvider),
8885
registerRenameSupport(selector, workspace, referencesProvider, parser.slugifier),
89-
registerSmartSelectSupport(selector, parser, tocProvider),
9086
registerWorkspaceSymbolSupport(workspace, symbolProvider),
9187
);
9288
}

extensions/markdown-language-features/src/languageFeatures/folding.ts

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)