Skip to content

Commit

Permalink
Merge pull request #157 from rjmacarthy/development
Browse files Browse the repository at this point in the history
Simplify duplicate sequences
  • Loading branch information
rjmacarthy authored Mar 3, 2024
2 parents f4d4a81 + 30b7796 commit 7bf3858
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 67 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "twinny",
"displayName": "twinny - AI Code Completion and Chat",
"description": "Locally hosted AI code completion plugin for vscode",
"version": "3.7.8",
"version": "3.7.9",
"icon": "assets/icon.png",
"keywords": [
"code-inference",
Expand Down
81 changes: 21 additions & 60 deletions src/extension/completion-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,62 +102,31 @@ export class CompletionFormatter {

private normalise = (text: string) => text?.trim()

private removeDuplicateText = () => {
const normalisedAfter = this.normalise(this._textAfterCursor)
if (
(normalisedAfter &&
this._normalisedCompletion &&
normalisedAfter === this._normalisedCompletion) ||
(this._textAfterCursor &&
(!this._completion.length &&
this._normalisedCompletion.endsWith(this._textAfterCursor)))
) {
this._completion = ''
return this
}

if (
this._normalisedCompletion &&
normalisedAfter &&
this._normalisedCompletion.includes(normalisedAfter)
) {
if (QUOTES.includes(normalisedAfter.at(-1) as string)) {
this._completion = this._completion.replace(
normalisedAfter.slice(0, -1),
''
)
return this
private removeDuplicateText() {
const after = this.normalise(this._textAfterCursor)

const maxLength = Math.min(this._completion.length, after.length)
let overlapLength = 0

for (let length = 1; length <= maxLength; length++) {
const endOfCompletion = this._completion.substring(
this._completion.length - length
)
const startOfAfter = after.substring(0, length)
if (endOfCompletion === startOfAfter) {
overlapLength = length
}

this._completion = this._completion.replace(normalisedAfter, '')
return this
}

if (
this._normalisedCompletion &&
normalisedAfter.includes(this._normalisedCompletion)
) {
const before = normalisedAfter.at(
normalisedAfter.indexOf(this._normalisedCompletion) - 1
) as string
const after = normalisedAfter.at(
normalisedAfter.indexOf(this._normalisedCompletion) +
this._normalisedCompletion.length
) as string
if (this.isMiddleWord(before, after)) {
return this
}
this._completion = ''
return this
if (overlapLength > 0) {
this._completion = this._completion.substring(
0,
this._completion.length - overlapLength
)
}

return this
}

private isMiddleWord(before: string, after: string) {
return before && after && /\w/.test(before) && /\w/.test(after)
}

private isCursorAtMiddleOfWord() {
return (
this._charAfterCursor &&
Expand Down Expand Up @@ -247,16 +216,6 @@ export class CompletionFormatter {
return this
}

private checkBrackets = (): CompletionFormatter => {
if (
this.isOnlyBrackets(this._normalisedCompletion) ||
this.isSingleBracket(this._normalisedCompletion)
) {
this._completion = this._normalisedCompletion
}
return this
}

private skipMiddleOfWord() {
if (this.isCursorAtMiddleOfWord()) {
this._completion = ''
Expand All @@ -265,6 +224,9 @@ export class CompletionFormatter {
}

private getCompletion = () => {
if (this._completion.trim().length === 0) {
this._completion = ''
}
return this._completion
}

Expand All @@ -285,7 +247,6 @@ export class CompletionFormatter {
.removeUnnecessaryMiddleQuote()
.ignoreBlankLines()
.removeInvalidLineBreaks()
.checkBrackets()
.removeDuplicateText()
.skipMiddleOfWord()
.getCompletion()
Expand Down
15 changes: 11 additions & 4 deletions src/extension/providers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import {
StreamResponse
} from '../../common/types'
import { getFimPrompt, getStopWords } from '../fim-templates'
import {
LINE_BREAK_REGEX,
MAX_CONTEXT_LINE_COUNT
} from '../../common/constants'
import { LINE_BREAK_REGEX, MAX_CONTEXT_LINE_COUNT } from '../../common/constants'
import { streamResponse } from '../stream'
import { createStreamRequestBody } from '../model-options'
import { Logger } from '../../common/logger'
Expand Down Expand Up @@ -101,6 +98,11 @@ export class CompletionProvider implements InlineCompletionItemProvider {
document,
position
)

if (this.isMiddleWord(prefixSuffix)) {
return []
}

const prompt = await this.getPrompt(prefixSuffix)
const cachedCompletion = cache.getCache(prefixSuffix)

Expand Down Expand Up @@ -140,6 +142,11 @@ export class CompletionProvider implements InlineCompletionItemProvider {
})
}

private isMiddleWord(prefixSuffix: PrefixSuffix) {
const { prefix, suffix } = prefixSuffix
return /\w/.test(prefix.at(-1) as string) && /\w/.test(suffix.at(0) as string)
}

private buildStreamRequest(prompt: string) {
const requestBody = createStreamRequestBody(this._apiProvider, prompt, {
model: this._fimModel,
Expand Down

0 comments on commit 7bf3858

Please sign in to comment.