Skip to content

Commit

Permalink
trigger completion for trigger with none keywords
Browse files Browse the repository at this point in the history
Closes #330
  • Loading branch information
chemzqm committed Sep 7, 2023
1 parent b2735e0 commit c2c0cfa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,29 @@ export class ProviderManager implements CompletionItemProvider {
let before_content = currline.slice(0, character)
let after = line.slice(characterIndex(line, colnr - 1))
let res: CompletionItem[] = []
let noneWords = before_content.endsWith(' ') ? '' : before_content.match(/\W*$/)[0]
for (let snip of snippets) {
// Avoid context during completion.
if (snip.context || snip.prefix === '') continue
if (input.length == 0 && !before_content.endsWith(snip.prefix)) continue
let contentBehind = before_content
let contentBefore = before_content
let head = this.getPrefixHead(doc, snip.prefix)
let ultisnip = snip.provider == 'ultisnips' || snip.provider == 'snipmate'
let startCharacter = character
let item: CompletionItem = {
label: snip.prefix,
kind: CompletionItemKind.Snippet,
filterText: snip.prefix,
detail: snip.description,
insertTextFormat: InsertTextFormat.Snippet
}
// check common begin for special characters
if (noneWords && snip.special && noneWords.endsWith(snip.special)) {
let len = snip.special.length
item.filterText = item.filterText.slice(0, -len)
startCharacter = character - len
contentBefore = contentBefore.slice(0, -len)
}
item.data = {
snip,
provider: snip.provider,
Expand All @@ -148,7 +157,7 @@ export class ProviderManager implements CompletionItemProvider {
item.data.ultisnip = {
context: snip.context,
regex: snip.originRegex,
range: Range.create(position.line, character, position.line, character + snip.prefix.length),
range: Range.create(position.line, startCharacter, position.line, character + snip.prefix.length),
line: before_content + snip.prefix + after
}
}
Expand All @@ -158,33 +167,33 @@ export class ProviderManager implements CompletionItemProvider {
let ms = content.match(snip.regex)
if (!ms) continue
} else if (head && before_content.endsWith(head)) {
contentBehind = before_content.slice(0, - head.length)
contentBefore = before_content.slice(0, - head.length)
Object.assign(item, {
textEdit: {
range: Range.create({ line: position.line, character: character - head.length }, position),
range: Range.create({ line: position.line, character: startCharacter - head.length }, position),
newText: snip.prefix
}
})
} else if (input.length == 0) {
let { prefix } = snip
contentBehind = before_content.slice(0, - prefix.length)
contentBefore = before_content.slice(0, - prefix.length)
Object.assign(item, {
preselect: true,
textEdit: {
range: Range.create({ line: position.line, character: character - prefix.length }, position),
range: Range.create({ line: position.line, character: startCharacter - prefix.length }, position),
newText: snip.prefix
}
})
}
if (snip.triggerKind == TriggerKind.LineBegin && contentBehind.trim().length) continue
if (snip.triggerKind == TriggerKind.LineBegin && contentBefore.trim().length) continue
if (snip.triggerKind == TriggerKind.SpaceBefore) {
if (contentBehind.length && !/\s/.test(contentBehind[contentBehind.length - 1])) {
if (contentBefore.length && !/\s/.test(contentBefore[contentBefore.length - 1])) {
continue
}
}
if (!item.textEdit) {
item.textEdit = {
range: Range.create({ line: position.line, character }, position),
range: Range.create({ line: position.line, character: startCharacter }, position),
newText: snip.prefix
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export interface Snippet {
readonly context?: string
readonly autoTrigger?: boolean
readonly originRegex?: string
// none word prefix of prefix
readonly special?: string
preExpand?: string
postExpand?: string
postJump?: string
Expand Down
2 changes: 2 additions & 0 deletions src/ultisnipsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ export default class UltiSnipsParser {
triggers.push(trigger)
}
for (let prefix of triggers) {
let ms = prefix.match(/^\W+/)
let snippet: Snippet = {
originRegex,
context: parsedContext ? parsedContext : (option.includes('e') ? ms[3] : undefined),
filepath,
filetype,
prefix: prefix,
special: ms == null ? undefined : ms[0],
autoTrigger: option.indexOf('A') !== -1,
lnum: lnum - preLines.length - 2,
triggerKind: getTriggerKind(option),
Expand Down

0 comments on commit c2c0cfa

Please sign in to comment.