Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add execContext-option to enable context-execution for completion #349

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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