wikiparse.LanguageService (EN) - bhsd-harry/wikiparser-node GitHub Wiki
Table of Contents
This is a global constructor added by the browser extension, which can be used as a language server for online editors such as CodeMirror and Monaco (demo).
This class inherits all the properties and methods of the LanguageService class which are available in the 🌐 browser. Only the methods of which the types of parameters or return values have changed are listed below.
Expand
param: string The wikitext content.
returns: Promise<ColorInformation[]>
List all color references found in the document. Only hex and rgb(a) colors are supported.
// provideDocumentColors (browser)
(async () => {
const lsp = new wikiparse.LanguageService();
assert.deepStrictEqual(
await lsp.provideDocumentColors(
'<p style="color: rgba(255, 0, 0, .7)">',
),
[
{
range: {
start: {line: 0, character: 17},
end: {line: 0, character: 36},
},
color: {red: 1, green: 0, blue: 0, alpha: 0.7},
},
],
);
assert.deepStrictEqual(
await lsp.provideDocumentColors(
'<poem style="color: #00ff00ff"/>',
),
[
{
range: {
start: {line: 0, character: 20},
end: {line: 0, character: 29},
},
color: {red: 0, green: 1, blue: 0, alpha: 1},
},
],
);
assert.deepStrictEqual(
await lsp.provideDocumentColors(
"{{#tag:font|color=#f000}}",
),
[
{
range: {
start: {line: 0, character: 18},
end: {line: 0, character: 23},
},
color: {red: 1, green: 0, blue: 0, alpha: 0},
},
],
);
assert.deepStrictEqual(
await lsp.provideDocumentColors(
"{{color|rgb(0 0 255 / 50%)}}",
),
[
{
range: {
start: {line: 0, character: 8},
end: {line: 0, character: 26},
},
color: {red: 0, green: 0, blue: 1, alpha: 0.5},
},
],
);
})();Expand
param: ColorPresentationParams
returns: Promise<ColorPresentation[]>
Obtain a list of presentations for a color value at a given location.
// provideColorPresentations (browser)
(async () => {
const lsp = new wikiparse.LanguageService({}),
range = {
start: {line: 0, character: 0},
end: {line: 0, character: 1},
};
assert.deepStrictEqual(
await lsp.provideColorPresentations({
color: {red: 1, green: 0, blue: 0, alpha: 0.5},
range,
}),
[
{
label: "rgba(255,0,0,0.5)",
textEdit: {range, newText: "rgba(255,0,0,0.5)"},
},
],
);
assert.deepStrictEqual(
await lsp.provideColorPresentations({
color: {red: 0, green: 0.9, blue: 0, alpha: 1},
range,
}),
[
{
label: "#00e600",
textEdit: {range, newText: "#00e600"},
},
],
);
})();Expand
param: string The wikitext content.
param: Position
returns: Promise<CompletionItem[] | undefined>
Compute completion items at a given cursor position.
// provideCompletionItems (browser)
(async () => {
const lsp = new wikiparse.LanguageService();
assert.deepStrictEqual(
(await lsp.provideCompletionItems("<Im", {line: 0, character: 3}))
?.filter(({label}) => label.startsWith("im")),
[
{
label: "imagemap",
kind: "Class",
textEdit: {
range: {
start: {line: 0, character: 1},
end: {line: 0, character: 3},
},
newText: "imagemap",
},
},
{
label: "img",
kind: "Class",
textEdit: {
range: {
start: {line: 0, character: 1},
end: {line: 0, character: 3},
},
newText: "img",
},
},
],
);
})();Expand
param: string The wikitext content.
returns: Promise<FoldingRange[]>
Return all folding ranges found in a given document.
// provideFoldingRanges (browser)
(async () => {
const lsp = new wikiparse.LanguageService(),
wikitext = `
<!--
-->= 1 =
<!-- -->
<!-- -->== 2 ==
===== 3 ===== <!--
-->
x {{a|
====== 4_<!--
-->2 ======
y }} z
== 4 ==
= 4 =
: {|
|
=== 4 ===
|} x
`;
assert.deepStrictEqual(
await lsp.provideFoldingRanges(wikitext),
[
{startLine: 15, endLine: 17, kind: "region"},
{startLine: 7, endLine: 19, kind: "region"},
{startLine: 11, endLine: 19, kind: "region"},
{startLine: 17, endLine: 19, kind: "region"},
{startLine: 3, endLine: 22, kind: "region"},
{startLine: 20, endLine: 22, kind: "region"},
{startLine: 25, endLine: 27, kind: "region"},
{startLine: 23, endLine: 30, kind: "region"},
{startLine: 27, endLine: 30, kind: "region"},
],
);
})();Expand
param: string The wikitext content.
returns: Promise<DocumentLink[]>
Request the location of links in a document.
// provideLinks (browser)
(async () => {
const lsp = new wikiparse.LanguageService();
assert.deepStrictEqual(
await lsp.provideLinks("RFC 1"),
[
{
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 5},
},
target: "https://datatracker.ietf.org/doc/html/rfc1",
},
],
);
})();Expand
param: string The wikitext content.
param: Position
returns: Promise<Omit<Location, 'uri'>[] | undefined>
Resolve references for the symbol denoted by the given document position.
// provideReferences (browser)
(async () => {
const lsp = new wikiparse.LanguageService(),
wikitext = `
{{{ a }}}
{{{a|}}}
`;
assert.deepStrictEqual(
await lsp.provideReferences(wikitext, {line: 1, character: 4}),
[
{
range: {
start: {line: 1, character: 3},
end: {line: 1, character: 6},
},
},
{
range: {
start: {line: 2, character: 3},
end: {line: 2, character: 4},
},
},
].reverse(),
);
})();Expand
param: string The wikitext content.
param: Position
returns: Promise<Omit<Location, 'uri'>[] | undefined>
Resolve the definition location of a symbol at a given document position.
// provideDefinition (browser)
(async () => {
const lsp = new wikiparse.LanguageService(),
wikitext = `
<ref group = f name = f > </ref>
<ref name = f > </ref>
<ref name = ' f ' />
`;
assert.deepStrictEqual(
await lsp.provideDefinition(wikitext, {line: 3, character: 14}),
[
{
range: {
start: {line: 2, character: 15},
end: {line: 2, character: 16},
},
},
],
);
})();Expand
param: string The wikitext content.
param: Position
returns: Promise<Range | undefined>
Test the validity of a rename operation at a given location.
// resolveRenameLocation (browser)
(async () => {
const lsp = new wikiparse.LanguageService();
assert.deepStrictEqual(
await lsp.resolveRenameLocation("{{{ a }}}", {line: 0, character: 4}),
{
start: {line: 0, character: 3},
end: {line: 0, character: 6},
},
);
})();Expand
param: string The wikitext content.
param: Position
param: string The new name.
returns: Promise<WorkspaceEdit | undefined>
Compute a change for a document-wide rename of a symbol.
// provideRenameEdits (browser)
(async () => {
const lsp = new wikiparse.LanguageService(),
wikitext = `
{{{ a }}}
{{{a|}}}
`;
assert.deepStrictEqual(
await lsp.provideRenameEdits(wikitext, {line: 1, character: 4}, "x"),
{
changes: {
"": [
{
range: {
start: {line: 1, character: 3},
end: {line: 1, character: 6},
},
newText: "x",
},
{
range: {
start: {line: 2, character: 3},
end: {line: 2, character: 4},
},
newText: "x",
},
].reverse(),
},
},
);
})();Expand
param: string The wikitext content.
param: boolean Whether to include warnings.
returns: Promise<Diagnostic[]>
Compute the syntax diagnostics for a given document.
// provideDiagnostics (browser)
(async () => {
const lsp = new wikiparse.LanguageService();
assert.deepStrictEqual(
await lsp.provideDiagnostics("http://a]"),
[
{
range: {
start: {line: 0, character: 8},
end: {line: 0, character: 9},
},
severity: 1,
source: "WikiLint",
code: "lonely-bracket",
message: 'lonely "]"',
data: [
{
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 0},
},
newText: "[",
title: "Suggestion: opening bracket",
fix: false,
},
],
},
],
);
assert.deepStrictEqual(
await lsp.provideDiagnostics("</p>"),
[
{
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 4},
},
severity: 2,
source: "WikiLint",
code: "unmatched-tag",
message: "unmatched closing tag",
data: [
{
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 4},
},
newText: "",
title: "Suggestion: remove",
fix: false,
},
],
},
],
);
assert.deepStrictEqual(
await lsp.provideDiagnostics("["),
[
{
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 1},
},
severity: 2,
source: "WikiLint",
code: "lonely-bracket",
message: 'lonely "["',
data: [],
},
],
);
assert.deepStrictEqual(
await lsp.provideDiagnostics("[", false),
[],
);
})();Expand
version added: 1.24.1
param: rule The rule to fix.
returns: Promise<CodeAction>
Compute the code action to fix all diagnostics of a given rule.
// resolveCodeAction (browser)
(async () => {
const lsp = new wikiparse.LanguageService();
assert.deepStrictEqual(
await lsp.provideDiagnostics("</br>"),
[
{
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 5},
},
severity: 1,
source: "WikiLint",
code: "unmatched-tag",
message: "tag that is both closing and self-closing",
data: [
{
range: {
start: {line: 0, character: 1},
end: {line: 0, character: 2},
},
newText: "",
title: "Fix: open",
fix: true,
},
],
},
],
);
assert.deepStrictEqual(
await lsp.resolveCodeAction("unmatched-tag"),
{
title: "Fix all: unmatched-tag",
kind: "source.fixAll",
data: {rule: "unmatched-tag"},
edit: {
changes: {
"": [
{
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 5},
},
newText: "<br>",
},
],
},
},
},
);
assert.deepStrictEqual(
await lsp.resolveCodeAction(),
{
title: "Fix all: WikiLint",
kind: "source.fixAll",
data: {rule: ""},
edit: {
changes: {
"": [
{
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 5},
},
newText: "<br>",
},
],
},
},
},
);
})();Expand
version added: 1.16.3
param: string The wikitext content.
returns: Promise<InlayHint[]>
Compute inlay hints for a given document tuple that may be rendered in the editor in place with other text.
// provideInlayHints (browser)
(async () => {
const lsp = new wikiparse.LanguageService();
assert.deepStrictEqual(
await lsp.provideInlayHints("{{a|b=|c}}"),
[
{
position: {line: 0, character: 7},
kind: 2,
label: "1=",
},
],
);
assert.deepStrictEqual(
await lsp.provideInlayHints("{{#invoke:a|b|c}}"),
[
{
position: {line: 0, character: 14},
kind: 2,
label: "1=",
},
].reverse(),
);
})();Expand
version added: 1.24.1
param: string The wikitext content.
param: Range The range to refactor.
returns: Promise<CodeAction[]>
Compute refactoring actions for a given document range.
// provideRefactoringAction (browser)
(async () => {
const lsp = new wikiparse.LanguageService();
assert.deepStrictEqual(
await lsp.provideRefactoringAction("=", {
start: {line: 0, character: 0},
end: {line: 0, character: 1},
}),
[
{
title: "Escape with magic words",
kind: "refactor.rewrite",
edit: {
changes: {
"": [
{
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 1},
},
newText: "{{=}}",
},
],
},
},
},
],
);
assert.deepStrictEqual(
await lsp.provideRefactoringAction("{|\n|}", {
start: {line: 0, character: 0},
end: {line: 1, character: 2},
}),
[
{
title: "Escape with magic words",
kind: "refactor.rewrite",
edit: {
changes: {
"": [
{
range: {
start: {line: 0, character: 0},
end: {line: 1, character: 2},
},
newText: "{{{!}}\n{{!}}}",
},
],
},
},
},
],
);
})();