Skip to content

Commit

Permalink
My tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-dyshel committed Mar 14, 2021
1 parent c3a2809 commit ace6161
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 21 deletions.
22 changes: 22 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "compile",
"isBackground": true,
"problemMatcher": [
"$tsc-watch"
],
"group": {
"kind": "build",
"isDefault": true
},
"runOptions": {
"runOn": "folderOpen"
}
}
]
}
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
[![Youtube Demo Video][youtube-shield]][youtube-url]
[![Twitter Follow][twitter-shield]][twitter-url]

# Fork tweaks

- Completion triggered only after alpha-numeric and symbols such as `.` or `->`.
This allows using TabNine with with other language completers.
- Configuration options to enable/disable TabNine per-language or per-project.
- Configurable positioning TabNine candidates in the beginning/end of
completion list (requires corresponding patch from my [VSCode fork](https://github.com/sergei-dyshel/vscode) to work).

# Original README

Tabnine is a powerful Artificial Intelligence assistant designed to help you code faster, reduce mistakes, and discover best coding practices - without ever leaving the comfort of VSCode.

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

24 changes: 23 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"publisher": "TabNine",
"name": "tabnine-vscode",
"version": "3.2.8",
"version": "99.2.8",
"displayName": "Tabnine Autocomplete AI: JavaScript, Python, TypeScript, PHP, Go, Java, Ruby, C/C++, HTML/CSS, C#, Rust, SQL, Bash, Kotlin, React",
"description": "👩‍💻🤖 JavaScript, Python, Java, Typescript & all other languages - AI Code completion plugin. Tabnine makes developers more productive by auto-completing their code.",
"icon": "small_logo.png",
Expand Down Expand Up @@ -431,6 +431,28 @@
"tabnine.logFilePath": {
"type": "string",
"description": "Tabnine's log file (Visual Studio Code must be restarted for this setting to take effect)"
},
"tabnine.positionInList": {
"type": "string",
"default": "inline",
"description": "Position of TabNine completions in the completion list",
"enum": [
"top",
"bottom",
"inline"
],
"scope": "resource"
},
"tabnine.enabled": {
"type": "boolean",
"default": true,
"description": "Whether TabNine is enabled",
"scope": "resource"
},
"tabnine.trigger": {
"type": "string",
"default": "off",
"description": "Trigger TabNine on every character, can be 'on', 'off' or comma-separated list of language IDs"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ColorThemeKind, ExtensionContext, Uri, window } from "vscode";
export const API_VERSION = "3.2.71";
export const BINARY_ROOT_PATH = path.join(__dirname, "..", "binaries");
export const ATTRIBUTION_BRAND = "⌬ ";
export const BRAND_NAME = "tabnine";
export const BRAND_NAME = "tabnine (fork)";
export const LIMITATION_SYMBOL = "🔒";
export const FULL_BRAND_REPRESENTATION = ATTRIBUTION_BRAND + BRAND_NAME;

Expand Down
81 changes: 68 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-restricted-syntax */
import * as vscode from "vscode";
import handleAlpha, { updatePersistedAlphaVersion } from "./alphaInstaller";
import pollDownloadProgress from "./binary/pollDownloadProgress";
Expand Down Expand Up @@ -31,12 +32,78 @@ import { registerStatusBar, setDefaultStatus } from "./statusBar/statusBar";
import { closeValidator } from "./validator/ValidatorClient";
import executeStartupActions from "./binary/startupActionsHandler";

let providerDisposers: vscode.Disposable[] = [];
let lastTrigger: string;
let channel: vscode.OutputChannel;

function getTrigger() {
return vscode.workspace.getConfiguration().get<string>('tabnine.trigger', 'off');
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function onConfigurationChange(_: vscode.ConfigurationChangeEvent) {
if (lastTrigger === getTrigger())
return;
lastTrigger = getTrigger();
void vscode.window.showInformationMessage('"tabnine.trigger" changed');
void registerAllProviders();
}

function complementLanguages(languages: string[], all: string[]): string[]
{
const allSet = new Set<string>(all);
for (const lang of languages)
allSet.delete(lang);
return Array.from(allSet);
}

async function registerAllProviders()
{
channel.appendLine('Unregistering all');
for (const disposer of providerDisposers)
disposer.dispose()
providerDisposers = []
const allLanguages = await vscode.languages.getLanguages();
if (lastTrigger === 'off') {
providerDisposers.push(...registerProvider(allLanguages, false));
} else if (lastTrigger === 'on') {
providerDisposers.push(...registerProvider(allLanguages, true));
} else {
const languages = lastTrigger.split(',').sort().map(lang => lang.trim());
providerDisposers.push(...registerProvider(languages, true));
providerDisposers.push(...registerProvider(
complementLanguages(languages, allLanguages), false));
}
}

function registerProvider(languages: string[], trigger: boolean) {
const triggers = trigger ? COMPLETION_TRIGGERS : [];

channel.appendLine(
`Registering trigger=${trigger} languages=${languages.join(', ')}`);

return [
vscode.languages.registerCompletionItemProvider(
languages, {
provideCompletionItems,
},
...triggers),
vscode.languages.registerHoverProvider(languages, {
provideHover,
})
]
}

export function activate(context: vscode.ExtensionContext): Promise<void> {
initBinary();
handleSelection(context);
handleUninstall(() => uponUninstall(context));

registerStatusBar(context);
channel = vscode.window.createOutputChannel('tabnine');
lastTrigger = getTrigger();
vscode.workspace.onDidChangeConfiguration(onConfigurationChange);


// Do not await on this function as we do not want VSCode to wait for it to finish
// before considering TabNine ready to operate.
Expand All @@ -61,19 +128,7 @@ async function backgroundInit(context: vscode.ExtensionContext) {
registerCommands(context);
pollDownloadProgress();
void executeStartupActions();
vscode.languages.registerCompletionItemProvider(
{ pattern: "**" },
{
provideCompletionItems,
},
...COMPLETION_TRIGGERS
);
vscode.languages.registerHoverProvider(
{ pattern: "**" },
{
provideHover,
}
);
await registerAllProviders();
}

export async function deactivate(): Promise<unknown> {
Expand Down
21 changes: 16 additions & 5 deletions src/provideCompletionItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ async function completionsListFor(
? 1
: response.results.length;

const config = vscode.workspace.getConfiguration(undefined, document.uri);
return response.results.slice(0, limit).map((entry, index) =>
makeCompletionItem({
config,
document,
index,
position,
Expand All @@ -88,6 +90,7 @@ function extractDetailMessage(response: AutocompleteResult) {
}

function makeCompletionItem(args: {
config: vscode.WorkspaceConfiguration,
document: vscode.TextDocument;
index: number;
position: vscode.Position;
Expand All @@ -105,8 +108,13 @@ function makeCompletionItem(args: {
} else {
item.detail = BRAND_NAME;
}

item.sortText = String.fromCharCode(0) + String.fromCharCode(args.index);
let sortChar = 'a';
const positionInList: 'top'|'bottom'|'inline' = args.config.get('tabnine.positionInList') ?? 'bottom';
if (positionInList === 'top')
sortChar = '0';
else if (positionInList === 'bottom')
sortChar = 'z';
item.sortText = sortChar + String.fromCharCode(args.index);
item.insertText = new vscode.SnippetString(
escapeTabStopSign(args.entry.new_prefix)
);
Expand All @@ -118,8 +126,8 @@ function makeCompletionItem(args: {
);
}
item.filterText = args.entry.new_prefix;
item.preselect = args.index === 0;
item.kind = args.entry.kind;
item.preselect = false;
item.kind = undefined;
item.range = new vscode.Range(
args.position.translate(0, -args.oldPrefix.length),
args.position.translate(0, args.entry.old_suffix.length)
Expand Down Expand Up @@ -191,7 +199,10 @@ function completionIsAllowed(
document: vscode.TextDocument,
position: vscode.Position
): boolean {
const configuration = vscode.workspace.getConfiguration();
const configuration =
vscode.workspace.getConfiguration(undefined, document.uri);
if (!configuration.get<boolean>('tabnine.enabled')) return false;

let disableLineRegex = configuration.get<string[]>(
"tabnine.disable_line_regex"
);
Expand Down

0 comments on commit ace6161

Please sign in to comment.