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 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(),
wikitext = `
<p style="color: rgba(255, 0, 0, .7)">
<poem style="color: #00ff00ff"/>
{{#tag:font|color=#f000}}
{{color|rgb(0 0 255 / 50%)}}
`;
assert.deepStrictEqual(
await lsp.provideDocumentColors(wikitext),
[
{
range: {
start: {line: 1, character: 17},
end: {line: 1, character: 36},
},
color: {red: 1, green: 0, blue: 0, alpha: 0.7},
},
{
range: {
start: {line: 2, character: 20},
end: {line: 2, character: 29},
},
color: {red: 0, green: 1, blue: 0, alpha: 1},
},
{
range: {
start: {line: 3, character: 18},
end: {line: 3, character: 23},
},
color: {red: 1, green: 0, blue: 0, alpha: 0},
},
{
range: {
start: {line: 4, character: 8},
end: {line: 4, character: 26},
},
color: {red: 0, green: 0, blue: 1, alpha: 0.5},
},
].reverse(),
);
})();
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: '#ff000080',
textEdit: {range, newText: '#ff000080'},
},
],
);
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://tools.ietf.org/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(),
wikitext = `
http://a]
</p>
`;
assert.deepStrictEqual(
await lsp.provideDiagnostics(wikitext),
[
{
range: {
start: {line: 1, character: 8},
end: {line: 1, character: 9},
},
severity: 1,
source: 'WikiLint',
code: 'lonely-bracket',
message: 'lonely "]"',
data: [
{
range: {
start: {line: 1, character: 0},
end: {line: 1, character: 0},
},
newText: '[',
title: 'Fix: left bracket',
fix: true,
},
],
},
{
range: {
start: {line: 2, character: 0},
end: {line: 2, character: 4},
},
severity: 1,
source: 'WikiLint',
code: 'unmatched-tag',
message: 'unmatched closing tag',
data: [
{
range: {
start: {line: 2, character: 0},
end: {line: 2, 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.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(),
wikitext = `
{{a|b=|c}}
{{#invoke:a|b|c}}
`;
assert.deepStrictEqual(
await lsp.provideInlayHints(wikitext),
[
{
position: {line: 1, character: 7},
kind: 2,
label: '1=',
},
{
position: {line: 2, character: 14},
kind: 2,
label: '1=',
},
].reverse(),
);
})();