Skip to content

Commit

Permalink
feat: vscode (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
bisquit authored Aug 10, 2023
1 parent cf1f8d3 commit 3ead6a8
Show file tree
Hide file tree
Showing 13 changed files with 846 additions and 77 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ fixtures/*
coverage

# turborepo
.turbo
.turbo

# vscode vsix
*.vsix
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// A launch configuration that compiles the extension and then opens it inside a new window
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [
{
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceFolder}/packages/vscode"],
"outFiles": ["${workspaceFolder}/packages/vscode/dist/**/*.js"]
}
]
}
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "0.1.2",
"license": "MIT",
"type": "module",
"sideEffects": false,
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { renameSymbols } from './renameSymbols.js';
export * from './renameSymbols.js';
5 changes: 2 additions & 3 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.2",
"license": "MIT",
"type": "module",
"sideEffects": false,
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
Expand All @@ -17,13 +18,11 @@
"build": "tsup"
},
"dependencies": {
"debug": "^4.3.4",
"shelljs": "^0.8.5"
"debug": "^4.3.4"
},
"devDependencies": {
"@types/debug": "^4.1.8",
"@types/node": "^20.4.7",
"@types/shelljs": "^0.8.12",
"tsup": "^7.1.0"
},
"repository": {
Expand Down
8 changes: 0 additions & 8 deletions packages/shared/src/copy.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './copy.js';
export * from './debug.js';
export * from './get-path-components.js';
export * from './get-path-components.js';
7 changes: 7 additions & 0 deletions packages/vscode/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 shuhei kikuchi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
94 changes: 94 additions & 0 deletions packages/vscode/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"name": "vscode-ts-rename-all",
"private": true,
"displayName": "TS Rename All",
"description": "Once rename, then others are also renamed the same way.",
"version": "0.1.2",
"license": "MIT",
"publisher": "bisquit",
"engines": {
"vscode": ">=1.80.0"
},
"categories": [],
"keywords": [],
"main": "./dist/client/extension.js",
"activationEvents": [],
"contributes": {
"commands": [
{
"command": "ts-rename-all.renameSymbols",
"title": "Rename all symbols in a file",
"category": "TS Rename All"
},
{
"command": "ts-rename-all.renameAll",
"title": "Rename all symbols",
"category": "TS Rename All"
}
],
"menus": {
"editor/context": [
{
"when": "editorLangId == typescript || editorLangId == typescriptreact",
"command": "ts-rename-all.renameSymbols"
}
],
"explorer/context": [
{
"command": "ts-rename-all.renameAll"
}
]
},
"configuration": {
"type": "object",
"title": "configuration",
"properties": {
"tsRenameAll.trace.server": {
"scope": "window",
"type": "string",
"enum": [
"off",
"messages",
"verbose"
],
"default": "off",
"description": "Traces the communication between VS Code and the language server."
}
}
}
},
"scripts": {
"dev": "concurrently --raw --kill-others pnpm:dev:*",
"dev:client": "pnpm build:client --watch",
"dev:server": "pnpm build:server --watch",
"build": "rimraf dist && pnpm build:client --minify && pnpm build:server --minify",
"build:client": "esbuild src/client/extension.ts --bundle --outfile=dist/client/extension.js --external:vscode --format=cjs --platform=node",
"build:server": "esbuild src/server/server.ts --bundle --outfile=dist/server/server.js --external:vscode --format=cjs --platform=node",
"lint": "eslint \"src/**/*.ts\"",
"typecheck": "tsc --noEmit",
"vsce:package": "pnpm vsce package --no-dependencies",
"vsce:publish": "pnpm vsce publish --no-dependencies"
},
"dependencies": {
"@ts-rename-all/core": "workspace:*",
"@ts-rename-all/shared": "workspace:*",
"vscode-languageclient": "^8.1.0",
"vscode-languageserver": "^8.1.0"
},
"devDependencies": {
"@types/node": "^20.4.7",
"@types/vscode": "^1.80.0",
"concurrently": "^8.2.0",
"esbuild": "^0.19.0",
"rimraf": "^5.0.1",
"typescript": "^5.1.5"
},
"repository": {
"type": "git",
"url": "https://github.com/bisquit/ts-rename-all.git",
"directory": "packages/vscode"
},
"bugs": {
"url": "https://github.com/bisquit/ts-rename-all/issues"
}
}
103 changes: 103 additions & 0 deletions packages/vscode/src/client/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import * as path from 'node:path';

import { ExtensionContext, workspace } from 'vscode';
import * as vscode from 'vscode';
import {
ExecuteCommandRequest,
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind,
} from 'vscode-languageclient/node';

let client: LanguageClient;

export async function activate(context: ExtensionContext) {
// The server is implemented in node
const serverModule = context.asAbsolutePath(
path.join('dist', 'server', 'server.js'),
);

// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
const serverOptions: ServerOptions = {
run: { module: serverModule, transport: TransportKind.ipc },
debug: {
module: serverModule,
transport: TransportKind.ipc,
// options: {
// execArgv: ['--nolazy', '--inspect=6009'],
// },
},
};

// Options to control the language client
const clientOptions: LanguageClientOptions = {
outputChannelName: 'TS Rename All',
};

// Create the language client and start the client.
client = new LanguageClient(
'tsRenameAll',
'TS Rename All',
serverOptions,
clientOptions,
);

// Start the client. This will also launch the server
await client.start();

context.subscriptions.push(
vscode.commands.registerCommand('ts-rename-all.renameSymbols', async () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}

const filePath = editor.document.uri.path;

const currentPosition = editor.selection.active;
const currentSymbolName = currentPosition
? editor.document.getText(
editor.document.getWordRangeAtPosition(currentPosition),
)
: '';

const destSymbolName = await vscode.window.showInputBox({
title: 'Type new name',
value: currentSymbolName,
});

// TODO: extract pattern

await client.sendRequest(ExecuteCommandRequest.type, {
command: 'ts-rename-all.renameSymbols',
arguments: [
{
srcFilePath: filePath,
srcSymbolPattern: currentSymbolName,
destSymbolPattern: destSymbolName,
},
],
});
}),
vscode.commands.registerCommand(
'ts-rename-all.renameAll',
async (uri?: vscode.Uri) => {
console.log('uri', uri);

await client.sendRequest(ExecuteCommandRequest.type, {
command: 'ts-rename-all.sample',
arguments: [uri],
});
},
),
);
}

export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}
return client.stop();
}
39 changes: 39 additions & 0 deletions packages/vscode/src/server/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { renameSymbols } from '@ts-rename-all/core';
import * as vscode from 'vscode';
import { createConnection, ProposedFeatures } from 'vscode-languageserver/node';

// Create a connection for the server, using Node's IPC as a transport.
// Also include all preview / proposed LSP features.
const connection = createConnection(ProposedFeatures.all);

connection.onInitialize(() => {
return {
capabilities: {},
};
});

connection.onExecuteCommand(async (params) => {
console.log('onExecuteCommand', params);

if (params.command === 'ts-rename-all.renameSymbols') {
const arg = params.arguments?.at(0) as {
srcFilePath: string;
srcSymbolPattern: string;
destSymbolPattern: string;
};
console.log('arg', arg);

await renameSymbols(arg.srcFilePath, {
srcSymbolPattern: arg.srcSymbolPattern,
destSymbolPattern: arg.destSymbolPattern,
});
}

if (params.command === 'ts-rename-all.sample') {
const arg = params.arguments?.at(0) as vscode.Uri;
console.log('arg', arg);
}
});

// Listen on the connection
connection.listen();
15 changes: 15 additions & 0 deletions packages/vscode/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS",
"moduleResolution": "Node16",
"esModuleInterop": true,
"resolveJsonModule": true,
"isolatedModules": true,
"strict": true,
"skipLibCheck": true,

"noEmit": true
},
"include": ["src"]
}
Loading

0 comments on commit 3ead6a8

Please sign in to comment.