-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Code actions + configuration nodes
- Loading branch information
1 parent
fd86c3a
commit 0a6b5e9
Showing
8 changed files
with
258 additions
and
180 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
clients/vscode-hlasmplugin/src/code_actions/configurationFilesActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) 2023 Broadcom. | ||
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Broadcom, Inc. - initial API and implementation | ||
*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import { relative } from 'path'; | ||
import { ConfigurationNodeDetails, ConfigurationNodes, anyConfigurationNodeExists } from '../configurationNodes'; | ||
import { proc_grps_file, pgm_conf_file } from '../constants' | ||
|
||
function codeActionFactory(create: boolean, filename: string | undefined, args: any[] | undefined): vscode.CodeAction { | ||
const filesDescription: string = filename ? filename + ' configuration file' : 'configuration files'; | ||
|
||
return { | ||
title: (create ? 'Create ' : 'Modify ') + filesDescription, | ||
command: { | ||
title: (create ? 'Create ' : 'Open ') + filesDescription, | ||
command: create ? 'extension.hlasm-plugin.createCompleteConfig' : 'vscode.open', | ||
arguments: args | ||
}, | ||
kind: vscode.CodeActionKind.QuickFix | ||
}; | ||
} | ||
|
||
function generateProcGrpsCodeAction(procGrps: ConfigurationNodeDetails, wsUri: vscode.Uri): vscode.CodeAction { | ||
return procGrps.exists ? codeActionFactory(false, proc_grps_file, [procGrps.uri]) : codeActionFactory(true, proc_grps_file, [wsUri, null, '']); | ||
} | ||
|
||
function generatePgmConfCodeAction(configNodes: ConfigurationNodes, wsUri: vscode.Uri, document: vscode.TextDocument): vscode.CodeAction { | ||
if (configNodes.pgmConf.exists) | ||
return codeActionFactory(false, pgm_conf_file, [configNodes.pgmConf.uri]); | ||
else { | ||
if (configNodes.bridgeJson.exists || configNodes.ebgFolder.exists) { | ||
// TODO: could we trigger B4G sync? | ||
} | ||
return codeActionFactory(true, pgm_conf_file, [wsUri, relative(wsUri.path, document.uri.path), null]); | ||
} | ||
} | ||
|
||
export function generateConfigurationFilesCodeActions(suggestProcGrpsChange: boolean, suggestPgmConfChange: boolean, configNodes: ConfigurationNodes, wsUri: vscode.Uri, document: vscode.TextDocument): vscode.CodeAction[] { | ||
if (!suggestProcGrpsChange && !suggestPgmConfChange) | ||
return []; | ||
|
||
if (!anyConfigurationNodeExists(configNodes)) | ||
return [codeActionFactory(true, undefined, [wsUri, relative(wsUri.path, document.uri.path), 'GRP1'])]; | ||
|
||
const result: vscode.CodeAction[] = []; | ||
|
||
if (suggestProcGrpsChange) | ||
result.push(generateProcGrpsCodeAction(configNodes.procGrps, wsUri)); | ||
|
||
if (suggestPgmConfChange) | ||
result.push(generatePgmConfCodeAction(configNodes, wsUri, document)); | ||
|
||
return result; | ||
} |
39 changes: 39 additions & 0 deletions
39
clients/vscode-hlasmplugin/src/code_actions/downloadDependenciesActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) 2023 Broadcom. | ||
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Broadcom, Inc. - initial API and implementation | ||
*/ | ||
|
||
import * as vscode from 'vscode'; | ||
|
||
export function generateDownloadDependenciesCodeActions(): vscode.CodeAction[] { | ||
const result: vscode.CodeAction[] = []; | ||
|
||
result.push({ | ||
title: 'Download missing dependencies', | ||
command: { | ||
title: 'Download dependencies', | ||
command: 'extension.hlasm-plugin.downloadDependencies', | ||
arguments: ['newOnly'] | ||
}, | ||
kind: vscode.CodeActionKind.QuickFix | ||
}); | ||
result.push({ | ||
title: 'Download all dependencies', | ||
command: { | ||
title: 'Download dependencies', | ||
command: 'extension.hlasm-plugin.downloadDependencies' | ||
}, | ||
kind: vscode.CodeActionKind.QuickFix | ||
}); | ||
|
||
return result; | ||
} |
78 changes: 78 additions & 0 deletions
78
clients/vscode-hlasmplugin/src/code_actions/opcodeSuggestionsActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (c) 2023 Broadcom. | ||
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Broadcom, Inc. - initial API and implementation | ||
*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import * as vscodelc from 'vscode-languageclient'; | ||
|
||
type OpcodeSuggestionList = { | ||
[key: string]: { | ||
opcode: string; | ||
distance: number; | ||
}[] | ||
}; | ||
|
||
interface OpcodeSuggestionResponse { | ||
uri: string; | ||
suggestions: OpcodeSuggestionList; | ||
}; | ||
|
||
function unique(a: string[]) { | ||
return [...new Set(a)]; | ||
} | ||
|
||
async function gatherOpcodeSuggestions(opcodes: string[], client: vscodelc.BaseLanguageClient, uri: vscode.Uri): Promise<OpcodeSuggestionList> { | ||
const suggestionsResponse = await client.sendRequest<OpcodeSuggestionResponse>("textDocument/$/opcode_suggestion", { | ||
textDocument: { uri: uri.toString() }, | ||
opcodes: opcodes, | ||
extended: false, | ||
}); | ||
|
||
return suggestionsResponse.suggestions; | ||
|
||
} | ||
|
||
async function opcodeTimeout(timeout: number): Promise<OpcodeSuggestionList> { | ||
return new Promise<OpcodeSuggestionList>((resolve, _) => { setTimeout(() => { resolve({}); }, timeout); }) | ||
} | ||
|
||
async function generateCodeActions(opcodeTasks: { | ||
diag: vscode.Diagnostic; | ||
opcode: string; | ||
}[], client: vscodelc.BaseLanguageClient, uri: vscode.Uri, timeout: number): Promise<vscode.CodeAction[]> { | ||
const result: vscode.CodeAction[] = []; | ||
const suggestions = await Promise.race([gatherOpcodeSuggestions(unique(opcodeTasks.map(x => x.opcode)), client, uri), opcodeTimeout(timeout)]); | ||
|
||
for (const { diag, opcode } of opcodeTasks) { | ||
if (opcode in suggestions) { | ||
const subst = suggestions[opcode]; | ||
for (const s of subst) { | ||
const edit = new vscode.WorkspaceEdit(); | ||
edit.replace(uri, diag.range, s.opcode) | ||
result.push({ | ||
title: `Did you mean '${s.opcode}'?`, | ||
diagnostics: [diag], | ||
kind: vscode.CodeActionKind.QuickFix, | ||
edit: edit | ||
}); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
const invalidUTF16Sequences = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g; | ||
|
||
export async function generateOpcodeSuggestionsCodeActions(diags: vscode.Diagnostic[], client: vscodelc.BaseLanguageClient, document: vscode.TextDocument): Promise<vscode.CodeAction[]> { | ||
return await generateCodeActions(diags.map(diag => { return { diag, opcode: document.getText(diag.range).replace(invalidUTF16Sequences, '').toUpperCase() }; }), client, document.uri, 1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2023 Broadcom. | ||
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Broadcom, Inc. - initial API and implementation | ||
*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import { uriExists } from './helpers' | ||
import { hlasmplugin_folder, proc_grps_file, pgm_conf_file, bridge_json_file, ebg_folder as ebg_folder } from './constants'; | ||
|
||
export interface ConfigurationNodeDetails { | ||
uri: vscode.Uri | typeof vscode.Uri; | ||
exists: boolean; | ||
} | ||
|
||
export interface ConfigurationNodes { | ||
procGrps: ConfigurationNodeDetails; | ||
pgmConf: ConfigurationNodeDetails; | ||
bridgeJson: ConfigurationNodeDetails; | ||
ebgFolder: ConfigurationNodeDetails; | ||
} | ||
|
||
export async function retrieveConfigurationNodes(workspace: vscode.Uri, documentUri: vscode.Uri | undefined, fs: vscode.FileSystem = vscode.workspace.fs): Promise<ConfigurationNodes> { | ||
const procGrps = vscode.Uri.joinPath(workspace, hlasmplugin_folder, proc_grps_file); | ||
const pgmConf = vscode.Uri.joinPath(workspace, hlasmplugin_folder, pgm_conf_file); | ||
const bridgeJson = documentUri ? vscode.Uri.joinPath(documentUri, "..", bridge_json_file) : undefined; | ||
const ebgFolder = vscode.Uri.joinPath(workspace, ebg_folder); | ||
return Promise.all([ | ||
uriExists(procGrps, fs).then(b => { return { uri: procGrps, exists: b }; }), | ||
uriExists(pgmConf, fs).then(b => { return { uri: pgmConf, exists: b }; }), | ||
bridgeJson ? uriExists(bridgeJson, fs).then(b => { return { uri: bridgeJson, exists: b }; }) : { uri: vscode.Uri, exists: false }, | ||
uriExists(ebgFolder, fs).then(b => { return { uri: ebgFolder, exists: b }; }), | ||
]).then(arr => { return { procGrps: arr[0], pgmConf: arr[1], bridgeJson: arr[2], ebgFolder: arr[3] }; }); | ||
} | ||
|
||
export function anyConfigurationNodeExists(configNodes: ConfigurationNodes) { | ||
return configNodes.procGrps.exists || configNodes.pgmConf.exists || configNodes.bridgeJson.exists || configNodes.ebgFolder.exists; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.