-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add configuration widget intellisense
- Loading branch information
1 parent
b37a099
commit 3142a15
Showing
2 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { CompletionItem, CompletionItemKind, CompletionItemProvider, SnippetString } from 'vscode'; | ||
|
||
interface ConfigurationTags { | ||
description: string; | ||
detail: string; | ||
snippet: string; | ||
} | ||
|
||
const configurationTags: Record<string, ConfigurationTags> = { | ||
o3rWidget: { | ||
description: 'Tag to use CMS widget for configuration', | ||
detail: 'widgetName', | ||
// TODO compute snippet thanks to eslint config to propose only valid widgetName | ||
snippet: '${1:widgetName}' | ||
}, | ||
o3rWidgetParam: { | ||
description: 'Tag to use CMS widget parameter for configuration', | ||
detail: 'paramName paramValue', | ||
// TODO compute snippet thanks to eslint config to propose only valid paramName and paramValue | ||
snippet: '${1:paramName} ${2:paramValue}' | ||
} | ||
}; | ||
|
||
const completions = Object.entries(configurationTags).map(([label, { detail, description, snippet }]) => { | ||
const completion = new CompletionItem({ label: `${label} `, detail }, CompletionItemKind.Keyword); | ||
completion.documentation = description; | ||
completion.insertText = new SnippetString(`${label} ${snippet}`); | ||
|
||
return completion; | ||
}); | ||
|
||
export const configurationCompletionItemProvider: CompletionItemProvider<CompletionItem> = { | ||
provideCompletionItems: (doc, pos) => { | ||
const line = doc.lineAt(pos).text; | ||
let prefix = line.slice(0, pos.character); | ||
if (!prefix.includes('@')) { | ||
return []; | ||
} | ||
|
||
const txt = doc.getText(); | ||
const jsDocCommentMatcher = /\/\*\*[^*](?:\r|\n|.)*?\*\//g; | ||
let match = jsDocCommentMatcher.exec(txt); | ||
let offset = doc.offsetAt(pos); | ||
while (match) { | ||
if (match.index > offset) { | ||
match = null; | ||
break; | ||
} | ||
if (match.index < offset && match.index + match[0].length > offset) { | ||
break; | ||
} | ||
match = jsDocCommentMatcher.exec(txt); | ||
} | ||
if (!match) { | ||
return []; | ||
} | ||
offset = prefix.lastIndexOf('@'); | ||
prefix = prefix.slice(offset + 1); | ||
if (prefix.match(/\s/)) { | ||
return []; | ||
} | ||
return completions; | ||
} | ||
}; |