Skip to content

Commit

Permalink
Fix the vscode-languageserver-* imports
Browse files Browse the repository at this point in the history
This package was importing code from `vscode-languageserver`. However,
it doesn’t specify `vscode-languageserver` as a dependency. All imports
used, were just re-exports from the `vscode-languageserver-protocol`
package. It was also importing code from the
`vscode-languageserver-types` package.

This changes all imports from `vscode-languageserver` and
`vscode-languageserver-types` to `vscode-languageserver-protocol`
imports, and adds this mising dependency.

Also the `vscode-languageserver-textdocument` dependency was moved into
`devDependencies`, because it’s only used in a test and in the example.
  • Loading branch information
remcohaszing committed Feb 13, 2024
1 parent 3997f63 commit bbd585e
Show file tree
Hide file tree
Showing 45 changed files with 158 additions and 221 deletions.
12 changes: 6 additions & 6 deletions example.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const mdls = require('.');
const MarkdownIt = require('markdown-it');
const { URI } = require('vscode-uri');
const { CancellationTokenSource, Emitter } = require('vscode-languageserver');
const { CancellationTokenSource, Emitter } = require('vscode-languageserver-protocol');
const { TextDocument } = require('vscode-languageserver-textdocument');

// First we need to create the services that the markdown language service depends on.
Expand All @@ -16,12 +16,12 @@ const mdIt = MarkdownIt({ html: true });

/** @type {mdls.IMdParser} */
const parser = new class {
slugifier = mdls.githubSlugifier
slugifier = mdls.githubSlugifier;

async tokenize(document) {
return mdIt.parse(document.getText(), {});
}
}
};

// Create a virtual document that holds our file content
const myDocument = TextDocument.create(
Expand Down Expand Up @@ -73,7 +73,7 @@ const workspace = new class {

async readDirectory(/** @type {URI} */resource) {
// Not implemented
return []
return [];
}


Expand Down Expand Up @@ -106,10 +106,10 @@ async function main() {
const cts = new CancellationTokenSource();
try {
const symbols = await languageService.getDocumentSymbols(myDocument, { includeLinkDefinitions: true }, cts.token);
console.log(JSON.stringify(symbols, null, 2))
console.log(JSON.stringify(symbols, null, 2));
} finally {
cts.dispose();
}
}

main();
main();
54 changes: 9 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"@vscode/l10n": "^0.0.10",
"node-html-parser": "^6.1.5",
"picomatch": "^2.3.1",
"vscode-languageserver-textdocument": "^1.0.8",
"vscode-languageserver-types": "^3.17.3",
"vscode-languageserver-protocol": "^3.17.1",
"vscode-uri": "^3.0.7"
},
"devDependencies": {
Expand All @@ -35,7 +34,7 @@
"mkdirp": "^1.0.4",
"mocha": "^10.0.0",
"typescript": "^5.1.3",
"vscode-languageserver": "^8.0.1"
"vscode-languageserver-textdocument": "^1.0.8"
},
"scripts": {
"api-extractor": "mkdirp etc && npx api-extractor run --local",
Expand Down
47 changes: 23 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import type { CancellationToken } from 'vscode-languageserver';
import * as lsp from 'vscode-languageserver-types';
import * as lsp from 'vscode-languageserver-protocol';
import { URI } from 'vscode-uri';
import { getLsConfiguration, LsConfiguration } from './config';
import { MdExtractLinkDefinitionCodeActionProvider } from './languageFeatures/codeActions/extractLinkDef';
Expand Down Expand Up @@ -50,7 +49,7 @@ export interface IMdLanguageService {
*
* Note that you must invoke {@link IMdLanguageService.resolveDocumentLink} on each link before executing the link.
*/
getDocumentLinks(document: ITextDocument, token: CancellationToken): Promise<lsp.DocumentLink[]>;
getDocumentLinks(document: ITextDocument, token: lsp.CancellationToken): Promise<lsp.DocumentLink[]>;

/**
* Resolves a link from {@link IMdLanguageService.getDocumentLinks}.
Expand All @@ -59,7 +58,7 @@ export interface IMdLanguageService {
*
* @returns The resolved link or `undefined` if the passed in link should be used
*/
resolveDocumentLink(link: lsp.DocumentLink, token: CancellationToken): Promise<lsp.DocumentLink | undefined>;
resolveDocumentLink(link: lsp.DocumentLink, token: lsp.CancellationToken): Promise<lsp.DocumentLink | undefined>;

/**
* Try to resolve the resources that a link in a markdown file points to.
Expand All @@ -69,14 +68,14 @@ export interface IMdLanguageService {
*
* @returns The resolved target or undefined if it could not be resolved.
*/
resolveLinkTarget(linkText: string, fromResource: URI, token: CancellationToken): Promise<ResolvedDocumentLinkTarget | undefined>;
resolveLinkTarget(linkText: string, fromResource: URI, token: lsp.CancellationToken): Promise<ResolvedDocumentLinkTarget | undefined>;

/**
* Get the symbols of a markdown file.
*
* @returns The headers and optionally also the link definitions in the file
*/
getDocumentSymbols(document: ITextDocument, options: { readonly includeLinkDefinitions?: boolean }, token: CancellationToken): Promise<lsp.DocumentSymbol[]>;
getDocumentSymbols(document: ITextDocument, options: { readonly includeLinkDefinitions?: boolean }, token: lsp.CancellationToken): Promise<lsp.DocumentSymbol[]>;

/**
* Get the folding ranges of a markdown file.
Expand All @@ -87,43 +86,43 @@ export interface IMdLanguageService {
* - Regions
* - List and other block element
*/
getFoldingRanges(document: ITextDocument, token: CancellationToken): Promise<lsp.FoldingRange[]>;
getFoldingRanges(document: ITextDocument, token: lsp.CancellationToken): Promise<lsp.FoldingRange[]>;

/**
* Get the selection ranges of a markdown file.
*/
getSelectionRanges(document: ITextDocument, positions: lsp.Position[], token: CancellationToken): Promise<lsp.SelectionRange[] | undefined>;
getSelectionRanges(document: ITextDocument, positions: lsp.Position[], token: lsp.CancellationToken): Promise<lsp.SelectionRange[] | undefined>;

/**
* Get the symbols for all markdown files in the current workspace.
*
* Returns all headers in the workspace.
*/
getWorkspaceSymbols(query: string, token: CancellationToken): Promise<lsp.WorkspaceSymbol[]>;
getWorkspaceSymbols(query: string, token: lsp.CancellationToken): Promise<lsp.WorkspaceSymbol[]>;

/**
* Get completions items at a given position in a markdown file.
*/
getCompletionItems(document: ITextDocument, position: lsp.Position, context: PathCompletionOptions, token: CancellationToken): Promise<lsp.CompletionItem[]>;
getCompletionItems(document: ITextDocument, position: lsp.Position, context: PathCompletionOptions, token: lsp.CancellationToken): Promise<lsp.CompletionItem[]>;

/**
* Get the references to a symbol at the current location.
*
* Supports finding references to headers and links.
*/
getReferences(document: ITextDocument, position: lsp.Position, context: lsp.ReferenceContext, token: CancellationToken): Promise<lsp.Location[]>;
getReferences(document: ITextDocument, position: lsp.Position, context: lsp.ReferenceContext, token: lsp.CancellationToken): Promise<lsp.Location[]>;

/**
* Get the references to a given file.
*/
getFileReferences(resource: URI, token: CancellationToken): Promise<lsp.Location[]>;
getFileReferences(resource: URI, token: lsp.CancellationToken): Promise<lsp.Location[]>;

/**
* Get the definition of the symbol at the current location.
*
* Supports finding headers from fragments links or reference link definitions.
*/
getDefinition(document: ITextDocument, position: lsp.Position, token: CancellationToken): Promise<lsp.Definition | undefined>;
getDefinition(document: ITextDocument, position: lsp.Position, token: lsp.CancellationToken): Promise<lsp.Definition | undefined>;

/**
* Organizes all link definitions in the file by grouping them to the bottom of the file, sorting them, and optionally
Expand All @@ -132,21 +131,21 @@ export interface IMdLanguageService {
* @returns A set of text edits. May be empty if no edits are required (e.g. the definitions are already sorted at
* the bottom of the file).
*/
organizeLinkDefinitions(document: ITextDocument, options: { readonly removeUnused?: boolean }, token: CancellationToken): Promise<lsp.TextEdit[]>;
organizeLinkDefinitions(document: ITextDocument, options: { readonly removeUnused?: boolean }, token: lsp.CancellationToken): Promise<lsp.TextEdit[]>;

/**
* Prepare for showing rename UI.
*
* Indicates if rename is supported. If it is, returns the range of symbol being renamed as well as the placeholder to show to the user for the rename.
*/
prepareRename(document: ITextDocument, position: lsp.Position, token: CancellationToken): Promise<{ range: lsp.Range; placeholder: string } | undefined>;
prepareRename(document: ITextDocument, position: lsp.Position, token: lsp.CancellationToken): Promise<{ range: lsp.Range; placeholder: string } | undefined>;

/**
* Get the edits for a rename operation.
*
* @returns A workspace edit that performs the rename or undefined if the rename cannot be performed.
*/
getRenameEdit(document: ITextDocument, position: lsp.Position, nameName: string, token: CancellationToken): Promise<lsp.WorkspaceEdit | undefined>;
getRenameEdit(document: ITextDocument, position: lsp.Position, nameName: string, token: lsp.CancellationToken): Promise<lsp.WorkspaceEdit | undefined>;

/**
* Get the edits for a file rename. This update links to the renamed files as well as links within the renamed files.
Expand All @@ -157,27 +156,27 @@ export interface IMdLanguageService {
*
* @returns An object with a workspace edit that performs the rename and a list of old file uris that effected the edit. Returns undefined if the rename cannot be performed.
*/
getRenameFilesInWorkspaceEdit(edits: readonly FileRename[], token: CancellationToken): Promise<{ participatingRenames: readonly FileRename[]; edit: lsp.WorkspaceEdit } | undefined>;
getRenameFilesInWorkspaceEdit(edits: readonly FileRename[], token: lsp.CancellationToken): Promise<{ participatingRenames: readonly FileRename[]; edit: lsp.WorkspaceEdit } | undefined>;

/**
* Get code actions for a selection in a file.
*
* Returned code actions may be disabled.
*/
getCodeActions(document: ITextDocument, range: lsp.Range, context: lsp.CodeActionContext, token: CancellationToken): Promise<lsp.CodeAction[]>;
getCodeActions(document: ITextDocument, range: lsp.Range, context: lsp.CodeActionContext, token: lsp.CancellationToken): Promise<lsp.CodeAction[]>;

/**
* Get document highlights for a position in the document.
*/
getDocumentHighlights(document: ITextDocument, position: lsp.Position, token: CancellationToken): Promise<lsp.DocumentHighlight[]>;
getDocumentHighlights(document: ITextDocument, position: lsp.Position, token: lsp.CancellationToken): Promise<lsp.DocumentHighlight[]>;

/**
* Compute diagnostics for a given file.
*
* Note that this function is stateless and re-validates all links every time you make the request. Use {@link IMdLanguageService.createPullDiagnosticsManager}
* to more efficiently get diagnostics.
*/
computeDiagnostics(doc: ITextDocument, options: DiagnosticOptions, token: CancellationToken): Promise<lsp.Diagnostic[]>;
computeDiagnostics(doc: ITextDocument, options: DiagnosticOptions, token: lsp.CancellationToken): Promise<lsp.Diagnostic[]>;

/**
* Create a stateful object that is more efficient at computing diagnostics across repeated calls and workspace changes.
Expand Down Expand Up @@ -259,24 +258,24 @@ export function createLanguageService(init: LanguageServiceInitialization): IMdL
getWorkspaceSymbols: workspaceSymbolProvider.provideWorkspaceSymbols.bind(workspaceSymbolProvider),
getCompletionItems: pathCompletionProvider.provideCompletionItems.bind(pathCompletionProvider),
getReferences: referencesProvider.provideReferences.bind(referencesProvider),
getFileReferences: async (resource: URI, token: CancellationToken): Promise<lsp.Location[]> => {
getFileReferences: async (resource: URI, token: lsp.CancellationToken): Promise<lsp.Location[]> => {
return (await referencesProvider.getReferencesToFileInWorkspace(resource, token)).map(x => x.location);
},
getDefinition: definitionsProvider.provideDefinition.bind(definitionsProvider),
organizeLinkDefinitions: organizeLinkDefinitions.getOrganizeLinkDefinitionEdits.bind(organizeLinkDefinitions),
prepareRename: renameProvider.prepareRename.bind(renameProvider),
getRenameEdit: renameProvider.provideRenameEdits.bind(renameProvider),
getRenameFilesInWorkspaceEdit: fileRenameProvider.getRenameFilesInWorkspaceEdit.bind(fileRenameProvider),
getCodeActions: async (doc: ITextDocument, range: lsp.Range, context: lsp.CodeActionContext, token: CancellationToken): Promise<lsp.CodeAction[]> => {
getCodeActions: async (doc: ITextDocument, range: lsp.Range, context: lsp.CodeActionContext, token: lsp.CancellationToken): Promise<lsp.CodeAction[]> => {
return (await Promise.all([
extractCodeActionProvider.getActions(doc, range, context, token),
Array.from(removeLinkDefinitionActionProvider.getActions(doc, range, context)),
])).flat();
},
getDocumentHighlights: (document: ITextDocument, position: lsp.Position, token: CancellationToken): Promise<lsp.DocumentHighlight[]> => {
getDocumentHighlights: (document: ITextDocument, position: lsp.Position, token: lsp.CancellationToken): Promise<lsp.DocumentHighlight[]> => {
return documentHighlightProvider.getDocumentHighlights(document, position, token);
},
computeDiagnostics: async (doc: ITextDocument, options: DiagnosticOptions, token: CancellationToken): Promise<lsp.Diagnostic[]> => {
computeDiagnostics: async (doc: ITextDocument, options: DiagnosticOptions, token: lsp.CancellationToken): Promise<lsp.Diagnostic[]> => {
return (await diagnosticsComputer.compute(doc, options, token))?.diagnostics;
},
createPullDiagnosticsManager: () => {
Expand Down
5 changes: 2 additions & 3 deletions src/languageFeatures/codeActions/extractLinkDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import * as l10n from '@vscode/l10n';
import { CancellationToken } from 'vscode-languageserver';
import * as lsp from 'vscode-languageserver-types';
import * as lsp from 'vscode-languageserver-protocol';
import { comparePosition, translatePosition } from '../../types/position';
import { makeRange, rangeIntersects } from '../../types/range';
import { getDocUri, getLine, ITextDocument } from '../../types/textDocument';
Expand Down Expand Up @@ -42,7 +41,7 @@ export class MdExtractLinkDefinitionCodeActionProvider {
this.#linkProvider = linkProvider;
}

async getActions(doc: ITextDocument, range: lsp.Range, context: lsp.CodeActionContext, token: CancellationToken): Promise<lsp.CodeAction[]> {
async getActions(doc: ITextDocument, range: lsp.Range, context: lsp.CodeActionContext, token: lsp.CancellationToken): Promise<lsp.CodeAction[]> {
if (!this.#isEnabled(context)) {
return [];
}
Expand Down
Loading

0 comments on commit bbd585e

Please sign in to comment.