Skip to content

Commit

Permalink
Add detect clones command (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanator authored Dec 16, 2020
1 parent 499905d commit 4fd7316
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@
"command": "sourcery.refactor.workspace",
"title": "Scan with Sourcery",
"category": "Sourcery"
},
{
"command": "sourcery.clones.workspace",
"title": "Detect clones",
"category": "Sourcery"
}
],
"menus": {
"explorer/context": [
{
"command": "sourcery.refactor.workspace",
"group": "1_modification"
"group": "1_modification@1"
},
{
"command": "sourcery.clones.workspace",
"group": "1_modification@2"
}
]
},
Expand Down
39 changes: 23 additions & 16 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ const REFACTOR_WORKSPACE_REQUEST = new RequestType('refactor_workspace');

function createLangServer(context: ExtensionContext): LanguageClient {

const token = workspace.getConfiguration("sourcery").get<string>("token");
const token = workspace.getConfiguration('sourcery').get<string>('token');
const packageJson = extensions.getExtension('sourcery.sourcery').packageJSON;
const extensionVersion = packageJson.version;
const sourceryVersion = packageJson.sourceryVersion;

const command = path.join(__dirname, "..", "binaries/sourcery-" + sourceryVersion + "-" + getOperatingSystem());

const serverOptions: ServerOptions = {
command,
args: ['lsp'],
options: {
env: {
PYTHONHASHSEED: "0",
PYTHONHASHSEED: '0',
...process.env
}
}
Expand All @@ -37,7 +37,7 @@ function createLangServer(context: ExtensionContext): LanguageClient {
const clientOptions: LanguageClientOptions = {
documentSelector: ['python'],
synchronize: {
configurationSection: "sourcery"
configurationSection: 'sourcery'
},
initializationOptions: {
'token': token,
Expand All @@ -48,7 +48,7 @@ function createLangServer(context: ExtensionContext): LanguageClient {

if (!token) {
const readmePath = Uri.file(
path.join(context.extensionPath, "INSTALL.py")
path.join(context.extensionPath, 'INSTALL.py')
);
window.showTextDocument(readmePath);
const result = window.showInputBox({
Expand All @@ -57,14 +57,14 @@ function createLangServer(context: ExtensionContext): LanguageClient {
ignoreFocusOut: true
});
result.then(function (value) {
workspace.getConfiguration("sourcery").update('token', value, true)
workspace.getConfiguration('sourcery').update('token', value, true)
});
}


return new LanguageClient(command, serverOptions, clientOptions);
}


function getOperatingSystem(): string {
if (process.platform == 'win32') {
return 'win/sourcery.exe'
Expand All @@ -76,25 +76,32 @@ function getOperatingSystem(): string {
}
}

let languageClient: LanguageClient;

export function activate(context: ExtensionContext) {
const languageClient = createLangServer(context)

const command = 'sourcery.refactor.workspace';

languageClient = createLangServer(context)
context.subscriptions.push(commands.registerCommand('sourcery.refactor.workspace', (resource: Uri, selected?: Uri[]) => {
let request: ExecuteCommandParams = {
command: 'refactor_workspace',
arguments: [{
'uri': resource,
'all_uris': selected
}]
};
languageClient.sendRequest(ExecuteCommandRequest.type, request);
}));

const commandHandler = (resource: Uri) => {
context.subscriptions.push(commands.registerCommand('sourcery.clones.workspace', (resource: Uri, selected?: Uri[]) => {
let request: ExecuteCommandParams = {
command: "refactor_workspace",
command: 'detect_clones',
arguments: [{
'uri': resource
'uri': resource,
'all_uris': selected
}]
};
languageClient.sendRequest(ExecuteCommandRequest.type, request);
};
}));

context.subscriptions.push(commands.registerCommand(command, commandHandler));
context.subscriptions.push(languageClient.start());
}

0 comments on commit 4fd7316

Please sign in to comment.