-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'HoleFiller-Autocomplete' into 'main'
Finish: Finish auto code completion See merge request nsysu_mis_projects/misb114/course_mis324!28
- Loading branch information
Showing
25 changed files
with
473 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 32 additions & 13 deletions
45
VSCodeExtension/code-brt/src/services/codeCompletion/providers/autoCodeCompletionProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,45 @@ | ||
import vscode from 'vscode'; | ||
import * as vscode from 'vscode'; | ||
|
||
import type { LoadedModelServices } from 'src/types'; | ||
import { AbstractCompletionProvider } from '../base'; | ||
import { SettingsManager } from '../../../api'; | ||
import { AutoCodeCompletionStrategy } from '../strategies'; | ||
import { StatusBarManager } from '../ui/statusBarManager'; | ||
import type { LoadedModelServices } from '../../../types'; | ||
|
||
// TODO: Implement the AutoCodeCompletionProvider class | ||
export class AutoCodeCompletionProvider implements AbstractCompletionProvider { | ||
private readonly completionStrategy: AutoCodeCompletionStrategy; | ||
|
||
constructor( | ||
_ctx: vscode.ExtensionContext, | ||
_settingsManager: SettingsManager, | ||
_loadedModelServices: LoadedModelServices, | ||
_statusBarManager: StatusBarManager, | ||
) {} | ||
ctx: vscode.ExtensionContext, | ||
settingsManager: SettingsManager, | ||
loadedModelServices: LoadedModelServices, | ||
statusBarManager: StatusBarManager, | ||
) { | ||
this.completionStrategy = new AutoCodeCompletionStrategy( | ||
ctx, | ||
settingsManager, | ||
loadedModelServices, | ||
statusBarManager, | ||
); | ||
} | ||
|
||
provideCompletionItems( | ||
_document: vscode.TextDocument, | ||
_position: vscode.Position, | ||
_token: vscode.CancellationToken, | ||
async provideCompletionItems( | ||
document: vscode.TextDocument, | ||
position: vscode.Position, | ||
token: vscode.CancellationToken, | ||
): Promise< | ||
vscode.InlineCompletionItem[] | vscode.InlineCompletionList | null | ||
> { | ||
return Promise.resolve(null); | ||
const completions = await this.completionStrategy.provideCompletion( | ||
document, | ||
position, | ||
token, | ||
); | ||
|
||
if (!completions || completions.length === 0) { | ||
return null; | ||
} | ||
|
||
return new vscode.InlineCompletionList(completions); | ||
} | ||
} |
186 changes: 185 additions & 1 deletion
186
...deExtension/code-brt/src/services/codeCompletion/strategies/autoCodeCompletionStrategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,185 @@ | ||
// TODO: Implement the auto code completion strategy by Code Model | ||
import * as vscode from 'vscode'; | ||
|
||
import type { | ||
CodeLanguageId, | ||
CompletionStrategy, | ||
CompletionTemplate, | ||
LoadedModelServices, | ||
ModelServiceType, | ||
} from '../../../types'; | ||
import { FILE_TO_LANGUAGE, LANGUAGE_NAME_MAPPING } from '../constants'; | ||
import { SettingsManager, HistoryManager } from '../../../api'; | ||
import { StatusBarManager } from '../ui/statusBarManager'; | ||
import { getTemplateForModel, postProcessCompletion } from '../utils'; | ||
|
||
export class AutoCodeCompletionStrategy implements CompletionStrategy { | ||
private readonly settingsManager: SettingsManager; | ||
private readonly historyManager: HistoryManager; | ||
private readonly loadedModelServices: LoadedModelServices; | ||
private readonly statusBarManager: StatusBarManager; | ||
|
||
constructor( | ||
ctx: vscode.ExtensionContext, | ||
settingsManager: SettingsManager, | ||
loadedModelServices: LoadedModelServices, | ||
statusBarManager: StatusBarManager, | ||
) { | ||
this.settingsManager = settingsManager; | ||
this.historyManager = new HistoryManager( | ||
ctx, | ||
'autoCodeCompletionIndex.json', | ||
'autoCodeCompletionHistories', | ||
); | ||
this.loadedModelServices = loadedModelServices; | ||
this.statusBarManager = statusBarManager; | ||
} | ||
|
||
private detectLanguageId( | ||
document: vscode.TextDocument, | ||
): CodeLanguageId | 'unknown' { | ||
const fileExtension = | ||
document.uri.fsPath.split('.').pop()?.toLowerCase() || ''; | ||
const languageIdFromExtension = FILE_TO_LANGUAGE[fileExtension]; | ||
const languageId = | ||
languageIdFromExtension || document.languageId.toLowerCase(); | ||
|
||
if (LANGUAGE_NAME_MAPPING[languageId as CodeLanguageId]) { | ||
return languageId as CodeLanguageId; | ||
} else { | ||
return 'unknown'; | ||
} | ||
} | ||
|
||
private cleanCompletionResponse(response: string): string { | ||
return response.trim(); | ||
} | ||
|
||
private async getResponse( | ||
prompt: string, | ||
modelService: ModelServiceType, | ||
modelName: string, | ||
template: CompletionTemplate, | ||
): Promise<string> { | ||
const history = this.historyManager.getCurrentHistory(); | ||
await this.historyManager.updateHistoryModelAdvanceSettings(history.root, { | ||
...history.advanceSettings, | ||
systemPrompt: '', | ||
temperature: template.completionOptions?.temperature || 0.7, | ||
maxTokens: template.completionOptions?.maxTokens || 150, | ||
stop: template.completionOptions?.stop || undefined, | ||
}); | ||
|
||
const response = await this.loadedModelServices[ | ||
modelService | ||
].service.getResponse({ | ||
query: prompt, | ||
historyManager: this.historyManager, | ||
selectedModelName: modelName, | ||
disableTools: true, | ||
}); | ||
|
||
return this.cleanCompletionResponse(response.textResponse); | ||
} | ||
|
||
public async provideCompletion( | ||
document: vscode.TextDocument, | ||
position: vscode.Position, | ||
token: vscode.CancellationToken, | ||
): Promise<vscode.InlineCompletionItem[] | null> { | ||
if (!this.settingsManager.get('autoTriggerCodeCompletion')) { | ||
return null; | ||
} | ||
|
||
this.statusBarManager.showProcessing(); | ||
|
||
try { | ||
const modelService = this.settingsManager.get( | ||
'lastUsedAutoCodeCompletionModelService', | ||
) as ModelServiceType; | ||
const modelName = this.settingsManager.get( | ||
'lastSelectedAutoCodeCompletionModel', | ||
)[modelService]; | ||
|
||
if (!this.loadedModelServices[modelService]) { | ||
console.warn(`Model service ${modelService} is not loaded.`); | ||
return null; | ||
} | ||
|
||
const languageId = this.detectLanguageId(document); | ||
if (languageId === 'unknown') { | ||
console.warn('Unsupported language for code completion.'); | ||
return null; | ||
} | ||
|
||
const language = LANGUAGE_NAME_MAPPING[languageId]; | ||
|
||
const template = getTemplateForModel(modelName); | ||
|
||
if (!template) { | ||
console.warn(`No template found for model ${modelName}`); | ||
return null; | ||
} | ||
|
||
const maxLines = 20; | ||
const startLine = Math.max(0, position.line - maxLines); | ||
const endLine = Math.min( | ||
document.lineCount - 1, | ||
position.line + maxLines, | ||
); | ||
|
||
const prefix = document.getText( | ||
new vscode.Range(new vscode.Position(startLine, 0), position), | ||
); | ||
|
||
const suffix = document.getText( | ||
new vscode.Range( | ||
position, | ||
new vscode.Position(endLine, Number.MAX_VALUE), | ||
), | ||
); | ||
|
||
const filepath = document.uri.fsPath; | ||
|
||
const prompt = | ||
typeof template.template === 'function' | ||
? template.template(prefix, suffix, filepath, language) | ||
: template.template | ||
.replace('{{{prefix}}}', prefix) | ||
.replace('{{{suffix}}}', suffix); | ||
|
||
if (token.isCancellationRequested) { | ||
return null; | ||
} | ||
|
||
const rawResponse = await this.getResponse( | ||
prompt, | ||
modelService, | ||
modelName, | ||
template, | ||
); | ||
|
||
const postProcessedResult = postProcessCompletion( | ||
rawResponse, | ||
prefix, | ||
suffix, | ||
modelName, | ||
); | ||
|
||
if (!postProcessedResult) { | ||
return null; | ||
} | ||
|
||
const completionItem = new vscode.InlineCompletionItem( | ||
postProcessedResult, | ||
new vscode.Range(position, position), | ||
); | ||
|
||
return [completionItem]; | ||
} catch (error) { | ||
console.error('Autocomplete error:', error); | ||
return null; | ||
} finally { | ||
this.statusBarManager.showIdle(); | ||
} | ||
} | ||
} |
11 changes: 1 addition & 10 deletions
11
VSCodeExtension/code-brt/src/services/codeCompletion/strategies/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,2 @@ | ||
import * as vscode from 'vscode'; | ||
|
||
export interface CompletionStrategy { | ||
provideCompletion( | ||
document: vscode.TextDocument, | ||
position: vscode.Position, | ||
token: vscode.CancellationToken, | ||
): Promise<vscode.InlineCompletionItem[] | null>; | ||
} | ||
|
||
export * from './manuallyCodeCompletionStrategy'; | ||
export * from './autoCodeCompletionStrategy'; |
Oops, something went wrong.