Skip to content

Commit

Permalink
add execContext-option to enable context-execution for completion
Browse files Browse the repository at this point in the history
  • Loading branch information
xi-lef committed Jan 27, 2024
1 parent 2a390b9 commit ceaf957
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ some regex patterns can't be supported by javascript, including
- `snippets.userSnippetsDirectory`, Directory that contains custom user ultisnips snippets, use ultisnips in extension root by default.
- `snippets.shortcut`, shortcut in completion menu, default `S`.
- `snippets.autoTrigger`: enable auto trigger for auto trigger ultisnips snippets, default `true`.
- `snippets.execContext`: execute a snippet's `context` (if it exists) to check if the snippet should be shown in completion menu, default `false` (i.e., snippets with a `context` are never shown in completion menu)
- `snippets.triggerCharacters`: trigger characters for completion, default `[]`.
- `snippets.loadFromExtensions`: load snippets from coc.nvim extensions, default: `true`.
- `snippets.loadVSCodeProjectSnippets`: Load code snippets in folder `${workspaceFolder}/.vscode`, default: `true`.
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@
"default": true,
"description": "Enable trigger auto trigger snippet after type character."
},
"snippets.execContext": {
"type": "boolean",
"default": false,
"description": "Execute a snippet's context (if it exists) to check if the snippet should be shown in completion menu"
},
"snippets.ultisnips.enable": {
"type": "boolean",
"default": true,
Expand Down
20 changes: 18 additions & 2 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,27 @@ export class ProviderManager implements CompletionItemProvider {
let after = line.slice(characterIndex(line, colnr - 1))
let res: CompletionItem[] = []
let noneWords = before_content.endsWith(' ') ? '' : before_content.match(/\W*$/)[0]
let contextPrefixes: string[] = []
const configuration = workspace.getConfiguration('snippets')
const execContext = configuration.get<boolean>('execContext', false)
for (let snip of snippets) {
// Avoid context during completion.
if (snip.context || snip.prefix === '') continue
if (!execContext && snip.context) continue
if (snip.prefix === '') continue
if (input.length == 0 && (!snip.special || !before_content.endsWith(snip.special))) continue
if (contextPrefixes.indexOf(snip.prefix) !== -1) continue
let contentBefore = before_content
if (snip.context) {
let provider = this.providers.get(snip.provider)
let valid: boolean
try {
valid = await provider.checkContext(snip.context)
} catch (e) {
this.appendError(`checkContext of ${snip.provider}`, e)
valid = false
}
if (!valid) continue
contextPrefixes.push(snip.prefix)
}
let head = this.getPrefixHead(doc, snip.prefix)
let ultisnip = snip.provider == 'ultisnips' || snip.provider == 'snipmate'
let startCharacter = character
Expand Down

0 comments on commit ceaf957

Please sign in to comment.