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

Simplify duplicate sequences #157

Merged
merged 2 commits into from
Mar 3, 2024
Merged
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
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