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 7, 2023
1 parent fe2c1d3 commit d8d0779
Show file tree
Hide file tree
Showing 11 changed files with 166 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,4 @@
{
"typescript.tsserver.log": "normal",
"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": {
"ts-error-translator-tssplugin": "file:..",
"typescript": "^4.8.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": "ts-error-translator-tssplugin"
}]
}
}
16 changes: 16 additions & 0 deletions packages/language-service-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ts-error-translator-tssplugin",
"version": "0.1.0",
"license": "MIT",
"main": "./out/index.js",
"private": true,
"files": ["./out/**"],
"scripts": {
"dev": "tsc --watch",
"build": "tsc"
},
"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"
]
}
118 changes: 70 additions & 48 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
packages:
- 'packages/*'
- 'packages/language-service-plugin/*'
- 'apps/*'

0 comments on commit d8d0779

Please sign in to comment.