Skip to content

Commit

Permalink
First draft of the language service plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
raqystyle committed Feb 6, 2023
1 parent fe2c1d3 commit ff5fe31
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"out": true // set this to false to include "out" folder in search results
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off"
"typescript.tsc.autoDetect": "off",
"typescript.tsserver.log": "normal"
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
9 changes: 9 additions & 0 deletions packages/language-service-plugin/example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let a;

export function A(b: number): number {
if (true) {
return 1;
} {
3;
}
}
8 changes: 8 additions & 0 deletions packages/language-service-plugin/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@total-typescript/language-service-plugin-example",
"version": "0.1.0",
"dependencies": {
"better-ts-errors": "file:..",
"typescript": "^4.5.3"
}
}
7 changes: 7 additions & 0 deletions packages/language-service-plugin/example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"plugins": [{
"name": "better-ts-errors"
}]
}
}
15 changes: 15 additions & 0 deletions packages/language-service-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "better-ts-errors",
"version": "0.1.0",
"license": "MIT",
"main": "./out/index.js",
"private": true,
"files": ["./out/**"],
"scripts": {
"dev": "tsc --watch"
},
"devDependencies": {
"tsconfig": "workspace:*",
"typescript": "^4.5.3"
}
}
37 changes: 37 additions & 0 deletions packages/language-service-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function init(modules: { typescript: typeof import("typescript/lib/tsserverlibrary") }) {
const ts = modules.typescript;

function create(info: ts.server.PluginCreateInfo) {
// Set up decorator object
const proxy: ts.LanguageService = Object.create(null);

for (let k of Object.keys(info.languageService) as Array<keyof ts.LanguageService>) {
const x = info.languageService[k]!;
// @ts-expect-error - JS runtime trickery which is tricky to type tersely
proxy[k] = (...args: Array<{}>) => x.apply(info.languageService, args);
}

// Remove specified entries from completion list
proxy.getCompletionsAtPosition = (fileName, position, options) => {
const prior = info.languageService.getCompletionsAtPosition(fileName, position, options);
if (!prior) {
return prior;
}
prior.entries = prior.entries.filter(e => e.name !== "caller");
const myItem: ts.CompletionEntry = {
name: 'MyTestKeyword',
kind: ts.ScriptElementKind.keyword,
sortText: 'My Test Keyword'
}
prior.entries.push(myItem);
return prior;
};

return proxy;
}

return { create };

}

export = init;
12 changes: 12 additions & 0 deletions packages/language-service-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"outDir": "./out"
},
"include": [
"src"
],
"exclude": [
"node_modules"
]
}
104 changes: 56 additions & 48 deletions pnpm-lock.yaml

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

0 comments on commit ff5fe31

Please sign in to comment.