Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dimacodota committed Dec 11, 2023
1 parent edf176a commit 7ab8393
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,37 @@ import {
CancellationToken,
CodeLens,
CodeLensProvider,
commands,
Diagnostic,
DiagnosticSeverity,
DocumentSymbol,
Event,
EventEmitter,
ExtensionContext,
languages,
Location,
SymbolInformation,
SymbolKind,
TextDocument,
Uri,
} from "vscode";
import { fireEvent } from "../../../binary/requests/requests";

const CODE_LENS_ACTIONS = [
["test", "generate-test-for-code"],
["fix", "fix-code"],
["explain", "explain-code"],
["document", "document-code"],
];
import { fireEvent } from "../../binary/requests/requests";
import { getFuctionsSymbols } from "./getFuctionsSymbols";
import { Action, COMANDS } from "./commands";
import tabnineExtensionProperties from "../../globals/tabnineExtensionProperties";
import { languagesFilter } from "./const";

const MAX_LINES = 2500;

export class ChatCodeLensProvider implements CodeLensProvider {
export default function registerChatCodeLens(context: ExtensionContext) {
if (!tabnineExtensionProperties.codeLensEnabled) {
return;
}
context.subscriptions.push(
languages.registerCodeLensProvider(
languagesFilter,
new ChatCodeLensProvider()
)
);
}

class ChatCodeLensProvider implements CodeLensProvider {
private visitedFiles: Set<Uri> = new Set();

private didChangeCodeLenses = new EventEmitter<void>();
Expand Down Expand Up @@ -63,7 +69,7 @@ export class ChatCodeLensProvider implements CodeLensProvider {
const lenses: CodeLens[] = [];

documnetSymbols.forEach(({ location }) => {
lenses.push(...toIntentLens(location), toAskLens(location));
lenses.push(...toIntentLens(location, diagnostic), toAskLens(location));
});

if (!this.visitedFiles.has(document.uri)) {
Expand Down Expand Up @@ -102,20 +108,26 @@ function toIntentLens(
location: Location,
diagnostics: Diagnostic[]
): CodeLens[] {
return CODE_LENS_ACTIONS.filter(([text]) =>
filterRelevantActions(text, location, diagnostics)
).map(
([text, action], index) =>
new CodeLens(location.range, {
title: `${index === 0 ? "tabnine: " : ""}${text}`,
tooltip: `tabnine ${text}`,
command: "tabnine.chat.commands.any",
arguments: [location.range, action],
})
return (
COMANDS.filter(
({ text, lensOrder }) =>
lensOrder && filterRelevantActions(text, location, diagnostics)
)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.sort((a, b) => a.lensOrder! - b.lensOrder!)
.map(
({ text, intent }, index) =>
new CodeLens(location.range, {
title: `${index === 0 ? "tabnine: " : ""}${text}`,
tooltip: `tabnine ${text}`,
command: "tabnine.chat.commands.any",
arguments: [location.range, intent],
})
)
);
}
function filterRelevantActions(
text: Intents,
text: Action,
location: Location,
diagnostics: Diagnostic[]
): boolean {
Expand Down
65 changes: 0 additions & 65 deletions src/tabnineChatWidget/extensionCommands/codeLens/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,43 +1,69 @@
type SCOPE = "block" | "selection" | "none";
export type SlashCommand = {
export type Action = "test" | "fix" | "explain" | "document" | "workspace";
export type Intent =
| "/generate-test-for-code"
| "/fix-code"
| "/explain-code"
| "/document-code"
| "/workspace";

enum LensOrder {
test = 1,
fix = 2,
explain = 3,
document = 4,
}

export type Command = {
label: string;
intent: string;
text: Action;
intent: Intent;
description: string;
scope: SCOPE[];
multistep: boolean;
lensOrder?: LensOrder | undefined;
};

export const SLASH_COMANDS: SlashCommand[] = [
export const COMANDS: Command[] = [
{
label: "$(feedback) explain",
text: "explain",
intent: "/explain-code",
description: "Explain the selected code",
scope: ["selection", "block"],
multistep: false,
lensOrder: LensOrder.explain,
},
{
label: "$(beaker) test",
text: "test",
intent: "/generate-test-for-code",
description: "Write tests for the selected code",
scope: ["block"],
multistep: false,
lensOrder: LensOrder.test,
},
{
label: "$(checklist) document",
text: "document",
intent: "/document-code",
description: "Add documentation for the selected code",
scope: ["block"],
multistep: false,
lensOrder: LensOrder.document,
},
{
label: "$(symbol-property) fix",
text: "fix",
intent: "/fix-code",
description: "Find errors in the selected code and fix them",
scope: ["block"],
multistep: false,
lensOrder: LensOrder.fix,
},
{
label: "$(search) workspace",
text: "workspace",
intent: "/workspace",
description:
"Ask a question related to any code within your current workspace",
Expand Down
54 changes: 51 additions & 3 deletions src/tabnineChatWidget/extensionCommands/registerChatCommnmads.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/* eslint-disable no-param-reassign */
import {
Disposable,
Range,
Selection,
SymbolInformation,
TextEditor,
commands,
window,
} from "vscode";
import ChatViewProvider from "../ChatViewProvider";
import { getFuctionsSymbols } from "./getFuctionsSymbols";
import { SLASH_COMANDS } from "./slashCommands";
import { COMANDS, Intent } from "./commands";
import { showInput } from "./showInput";
import { fireEvent } from "../../binary/requests/requests";

export function registerChatCommnmads(
chatProvider: ChatViewProvider
Expand Down Expand Up @@ -42,7 +45,7 @@ export function registerChatCommnmads(
commands.registerTextEditorCommand(
"tabnine.chat.commands.inline.action",
(textEditor: TextEditor) => {
void showInput(SLASH_COMANDS).then((result) => {
void showInput(COMANDS).then((result) => {
if (textEditor.selection.isEmpty) {
void getFuctionsSymbols(textEditor.document).then(
(relevantSymbols: SymbolInformation[]) => {
Expand All @@ -60,12 +63,52 @@ export function registerChatCommnmads(
);

if (result) {
void fireEvent({
name: "chat-ide-action",
intent: result,
language: window.activeTextEditor?.document.languageId,
});
void chatProvider.handleMessageSubmitted(result);
}
}
});
}
)
),
commands.registerCommand(
"tabnine.chat.commands.any",
(range: Range, intent: Intent) => {
void fireEvent({
name: "chat-lens-action",
intent,
language: window.activeTextEditor?.document.languageId,
});

const editor = window.activeTextEditor;
if (editor) {
const newSelection = new Selection(range.start, range.end);
editor.selection = newSelection;
}
void chatProvider.handleMessageSubmitted(intent);
}
),
commands.registerCommand("tabnine.chat.commands.ask", (range: Range) => {
void fireEvent({
name: "chat-lens-action",
intent: "ask-question",
language: window.activeTextEditor?.document.languageId,
});

const editor = window.activeTextEditor;
if (editor) {
const newSelection = new Selection(range.start, range.end);
editor.selection = newSelection;
}
void showInput(COMANDS).then((question) => {
if (question) {
void chatProvider.handleMessageSubmitted(question);
}
});
})
);
}

Expand All @@ -74,6 +117,11 @@ function contextActionHandler(
textEditor: TextEditor,
intent: string
): void {
void fireEvent({
name: "chat-ide-action",
intent,
language: window.activeTextEditor?.document.languageId,
});
if (textEditor.selection.isEmpty) {
void getFuctionsSymbols(textEditor.document).then(
(relevantSymbols: SymbolInformation[]) => {
Expand Down
4 changes: 2 additions & 2 deletions src/tabnineChatWidget/tabnineChatWidgetWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import ChatViewProvider from "./ChatViewProvider";
import { getState } from "../binary/requests/requests";
import { Logger } from "../utils/logger";
import { registerChatActionProvider } from "./extensionCommands/ChatActionProvider";
import registerChatCodeLens from "./extensionCommands/codeLens";
import { registerChatCommnmads } from "./extensionCommands/registerChatCommnmads";
import ChatEnabledState, { ChatNotEnabledReason } from "./ChatEnabledState";
import registerChatCodeLens from "./extensionCommands/ChatCodeLensProvider";

const VIEW_ID = "tabnine.chat";

Expand Down Expand Up @@ -103,7 +103,7 @@ function registerWebview(
);
context.subscriptions.push(registerChatCommnmads(chatProvider));
registerChatActionProvider(context);
registerChatCodeLens(context, chatProvider);
registerChatCodeLens(context);

hasRegisteredChatWebview = true;
}
Expand Down

0 comments on commit 7ab8393

Please sign in to comment.