Skip to content

Commit

Permalink
traverse back to find chracter before
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmacarthy committed Mar 8, 2024
1 parent 3f12698 commit f118276
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/extension/completion-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export class CompletionFormatter {
)
)

const score = this._completion.score(textAfter)
const score = textAfter.score(this._completion)

if (score > 0.5) this._completion = ''

Expand Down
19 changes: 16 additions & 3 deletions src/extension/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const getSkipVariableDeclataion = (
) {
return true
}

return false
}

Expand All @@ -109,6 +110,20 @@ export const getSkipImportDeclaration = (
return false
}

export const getCharacterBefore = (index = -1): string => {
const editor = window.activeTextEditor
if (!editor) return ''
const document = editor.document
const cursorPosition = editor.selection.active
const textBeforeRange = new Range(cursorPosition, new Position(0, 0))
const textBefore = document.getText(textBeforeRange)
const characterBefore = textBefore.at(index) as string
if (!characterBefore.trim()) {
return getCharacterBefore(index - 1)
}
return characterBefore
}

export const getShouldSkipCompletion = (
context: InlineCompletionContext,
disableAuto: boolean
Expand All @@ -119,10 +134,8 @@ export const getShouldSkipCompletion = (
const cursorPosition = editor.selection.active
const lineEndPosition = document.lineAt(cursorPosition.line).range.end
const textAfterRange = new Range(cursorPosition, lineEndPosition)
const textBeforeRange = new Range(cursorPosition, new Position(0, 0))
const textAfter = document.getText(textAfterRange)
const textBefore = document.getText(textBeforeRange)
const characterBefore = textBefore.at(-1) as string
const characterBefore = getCharacterBefore()
if (getSkipVariableDeclataion(characterBefore, textAfter)) return true
if (getSkipImportDeclaration(characterBefore, textAfter)) return true

Expand Down

0 comments on commit f118276

Please sign in to comment.