Skip to content

Commit 4fd7316

Browse files
authored
Add detect clones command (#25)
1 parent 499905d commit 4fd7316

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,22 @@
3131
"command": "sourcery.refactor.workspace",
3232
"title": "Scan with Sourcery",
3333
"category": "Sourcery"
34+
},
35+
{
36+
"command": "sourcery.clones.workspace",
37+
"title": "Detect clones",
38+
"category": "Sourcery"
3439
}
3540
],
3641
"menus": {
3742
"explorer/context": [
3843
{
3944
"command": "sourcery.refactor.workspace",
40-
"group": "1_modification"
45+
"group": "1_modification@1"
46+
},
47+
{
48+
"command": "sourcery.clones.workspace",
49+
"group": "1_modification@2"
4150
}
4251
]
4352
},

src/extension.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ const REFACTOR_WORKSPACE_REQUEST = new RequestType('refactor_workspace');
1616

1717
function createLangServer(context: ExtensionContext): LanguageClient {
1818

19-
const token = workspace.getConfiguration("sourcery").get<string>("token");
19+
const token = workspace.getConfiguration('sourcery').get<string>('token');
2020
const packageJson = extensions.getExtension('sourcery.sourcery').packageJSON;
2121
const extensionVersion = packageJson.version;
2222
const sourceryVersion = packageJson.sourceryVersion;
2323

2424
const command = path.join(__dirname, "..", "binaries/sourcery-" + sourceryVersion + "-" + getOperatingSystem());
25-
25+
2626
const serverOptions: ServerOptions = {
2727
command,
2828
args: ['lsp'],
2929
options: {
3030
env: {
31-
PYTHONHASHSEED: "0",
31+
PYTHONHASHSEED: '0',
3232
...process.env
3333
}
3434
}
@@ -37,7 +37,7 @@ function createLangServer(context: ExtensionContext): LanguageClient {
3737
const clientOptions: LanguageClientOptions = {
3838
documentSelector: ['python'],
3939
synchronize: {
40-
configurationSection: "sourcery"
40+
configurationSection: 'sourcery'
4141
},
4242
initializationOptions: {
4343
'token': token,
@@ -48,7 +48,7 @@ function createLangServer(context: ExtensionContext): LanguageClient {
4848

4949
if (!token) {
5050
const readmePath = Uri.file(
51-
path.join(context.extensionPath, "INSTALL.py")
51+
path.join(context.extensionPath, 'INSTALL.py')
5252
);
5353
window.showTextDocument(readmePath);
5454
const result = window.showInputBox({
@@ -57,14 +57,14 @@ function createLangServer(context: ExtensionContext): LanguageClient {
5757
ignoreFocusOut: true
5858
});
5959
result.then(function (value) {
60-
workspace.getConfiguration("sourcery").update('token', value, true)
60+
workspace.getConfiguration('sourcery').update('token', value, true)
6161
});
6262
}
6363

64-
6564
return new LanguageClient(command, serverOptions, clientOptions);
6665
}
6766

67+
6868
function getOperatingSystem(): string {
6969
if (process.platform == 'win32') {
7070
return 'win/sourcery.exe'
@@ -76,25 +76,32 @@ function getOperatingSystem(): string {
7676
}
7777
}
7878

79-
let languageClient: LanguageClient;
8079

8180
export function activate(context: ExtensionContext) {
81+
const languageClient = createLangServer(context)
8282

83-
const command = 'sourcery.refactor.workspace';
84-
85-
languageClient = createLangServer(context)
83+
context.subscriptions.push(commands.registerCommand('sourcery.refactor.workspace', (resource: Uri, selected?: Uri[]) => {
84+
let request: ExecuteCommandParams = {
85+
command: 'refactor_workspace',
86+
arguments: [{
87+
'uri': resource,
88+
'all_uris': selected
89+
}]
90+
};
91+
languageClient.sendRequest(ExecuteCommandRequest.type, request);
92+
}));
8693

87-
const commandHandler = (resource: Uri) => {
94+
context.subscriptions.push(commands.registerCommand('sourcery.clones.workspace', (resource: Uri, selected?: Uri[]) => {
8895
let request: ExecuteCommandParams = {
89-
command: "refactor_workspace",
96+
command: 'detect_clones',
9097
arguments: [{
91-
'uri': resource
98+
'uri': resource,
99+
'all_uris': selected
92100
}]
93101
};
94102
languageClient.sendRequest(ExecuteCommandRequest.type, request);
95-
};
103+
}));
96104

97-
context.subscriptions.push(commands.registerCommand(command, commandHandler));
98105
context.subscriptions.push(languageClient.start());
99106
}
100107

0 commit comments

Comments
 (0)