Skip to content

Commit

Permalink
refactor: Move constants
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored Mar 27, 2024
1 parent 3665e6a commit dc08a9b
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 237 deletions.
5 changes: 3 additions & 2 deletions clients/vscode-hlasmplugin/src/branchDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import * as vscode from 'vscode';
import * as vscodelc from 'vscode-languageclient';
import { HlasmPluginMiddleware } from './languageClientMiddleware';
import { languageIdHlasm } from './constants';

type BranchInfo = {
line: number, col: number, up: boolean, down: boolean, somewhere: boolean,
Expand Down Expand Up @@ -144,9 +145,9 @@ function initializeRequestHandling(client: vscodelc.BaseLanguageClient) {

function decorationsEnabled(document: vscode.TextDocument, expectedVersion: number | undefined = undefined) {
return !document.isClosed
&& document.languageId === 'hlasm'
&& document.languageId === languageIdHlasm
&& (expectedVersion === undefined || document.version === expectedVersion)
&& vscode.workspace.getConfiguration('hlasm', document).get<boolean>('showBranchInformation', true);
&& vscode.workspace.getConfiguration(languageIdHlasm, document).get<boolean>('showBranchInformation', true);
}

function ignoreFailure() { }
Expand Down
3 changes: 2 additions & 1 deletion clients/vscode-hlasmplugin/src/commentEditorCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

import * as vscode from 'vscode';
import { continuationColumn } from './constants';

export enum CommentOption {
toggle,
Expand All @@ -35,7 +36,7 @@ function seq(low: number, high: number): number[] {
}

function isContinued(text: string): boolean {
return text.length > 71 && ([...text][71] ?? ' ') !== ' ';
return text.length > continuationColumn && ([...text][continuationColumn] ?? ' ') !== ' ';
}

function findFirstLine(doc: vscode.TextDocument, lineno: number): number {
Expand Down
14 changes: 14 additions & 0 deletions clients/vscode-hlasmplugin/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,17 @@ export const ebg_folder = '.ebg';

export const hlasmplugin_folder_filter: vscode.DocumentFilter = { language: 'json', pattern: '**/.hlasmplugin/*' };
export const bridge_json_filter: vscode.DocumentFilter = { language: 'json', pattern: '**/.bridge.json' };

export const EXTENSION_ID = "broadcommfd.hlasm-language-support";

export const continuationColumn = 71;
export const initialBlanks = 15;

export const languageIdHlasm = 'hlasm';
export const languageIdHlasmListing = 'hlasmListing';

export const debugTypeHlasm = 'hlasm';

export const schemeExternalFiles = 'hlasm-external';
export const schemeVirtualFiles = 'hlasm';
export const schemeOutput = 'hlasm-output';
15 changes: 8 additions & 7 deletions clients/vscode-hlasmplugin/src/debugProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

import * as vscode from 'vscode';
import { debugTypeHlasm, languageIdHlasm } from './constants';

// debug configuration provider, adds port number dynamically
export class HLASMConfigurationProvider implements vscode.DebugConfigurationProvider {
Expand All @@ -21,10 +22,10 @@ export class HLASMConfigurationProvider implements vscode.DebugConfigurationProv
: vscode.ProviderResult<vscode.DebugConfiguration> {
// no launch.json, debug current
if (!config.type && !config.request && !config.name) {
config.type = "hlasm";
config.request = "launch";
config.name = "Macro tracer: current program";
config.program = "${command:extension.hlasm-plugin.getCurrentProgramName}";
config.type = debugTypeHlasm;
config.request = 'launch';
config.name = 'Macro tracer: current program';
config.program = '${command:extension.hlasm-plugin.getCurrentProgramName}';
config.stopOnEntry = true;
}
return config;
Expand All @@ -34,8 +35,8 @@ export class HLASMConfigurationProvider implements vscode.DebugConfigurationProv
// show an input box to select the program to trace
export function getProgramName() {
return vscode.window.showInputBox({
placeHolder: "Please enter the name of a program in the workspace folder",
value: "pgm"
placeHolder: 'Please enter the name of a program in the workspace folder',
value: 'pgm'
});
}

Expand All @@ -46,7 +47,7 @@ export function getCurrentProgramName() {
vscode.window.showErrorMessage('No file open.');
return undefined;
}
if (editor.document.languageId != 'hlasm') {
if (editor.document.languageId != languageIdHlasm) {
vscode.window.showErrorMessage(editor.document.fileName + ' is not a HLASM file.');
return undefined;
}
Expand Down
7 changes: 4 additions & 3 deletions clients/vscode-hlasmplugin/src/eventsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as vscode from 'vscode';
import { ConfigurationsHandler } from './configurationsHandler'
import { isLineContinued } from './customEditorCommands';
import { HLASMLanguageDetection } from './hlasmLanguageDetection'
import { languageIdHlasm } from './constants';

/**
* Handles various events happening in VSCode
Expand Down Expand Up @@ -49,11 +50,11 @@ export class EventsHandler {
// when contents of a document change, issue a completion request
onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent, continuationOffset: number): boolean {
if (getConfig<boolean>('continuationHandling', false)) {
if (event.document.languageId != 'hlasm')
if (event.document.languageId != languageIdHlasm)
return false;

//const editor = vscode.window.activeTextEditor;
if (event.contentChanges.length == 0 || event.document.languageId != "hlasm")
if (event.contentChanges.length == 0 || event.document.languageId != languageIdHlasm)
return false;

const change = event.contentChanges[0];
Expand Down Expand Up @@ -138,6 +139,6 @@ export class EventsHandler {
* @param defaultValue default value to return if option is not set
*/
export function getConfig<T>(option: string, defaultValue: T) {
const config = vscode.workspace.getConfiguration('hlasm');
const config = vscode.workspace.getConfiguration(languageIdHlasm);
return config.get<T>(option, defaultValue);
}
24 changes: 9 additions & 15 deletions clients/vscode-hlasmplugin/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { HLASMVirtualFileContentProvider } from './hlasmVirtualFileContentProvid
import { downloadDependencies } from './hlasmDownloadCommands';
import { blockCommentCommand, CommentOption, lineCommentCommand } from './commentEditorCommands';
import { HLASMCodeActionsProvider } from './hlasmCodeActionsProvider';
import { hlasmplugin_folder, hlasmplugin_folder_filter, bridge_json_filter } from './constants';
import { hlasmplugin_folder, hlasmplugin_folder_filter, bridge_json_filter, schemeExternalFiles, continuationColumn, initialBlanks, languageIdHlasm, debugTypeHlasm, schemeVirtualFiles } from './constants';
import { ConfigurationsHandler } from './configurationsHandler';
import { HlasmPluginMiddleware, getLanguageClientMiddleware } from './languageClientMiddleware';
import { HLASMExternalFiles } from './hlasmExternalFiles';
Expand All @@ -45,12 +45,6 @@ import { registerListingServices } from './hlasmListingServices';
import { MementoKey } from './mementoKeys';
import { registerOutputDocumentContentProvider, showOutputCommand } from './hlasmOutputContentProvider';

export const EXTENSION_ID = "broadcommfd.hlasm-language-support";

const continuationColumn = 71;
const initialBlanks = 15;
const externalFilesScheme = 'hlasm-external';

const sleep = (ms: number) => {
return new Promise((resolve) => { setTimeout(resolve, ms) });
};
Expand Down Expand Up @@ -111,7 +105,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<HlasmE
// create client options
const syncFileEvents = getConfig<boolean>('syncFileEvents', true);
const clientOptions: vscodelc.LanguageClientOptions = {
documentSelector: [{ language: 'hlasm' }],
documentSelector: [{ language: languageIdHlasm }],
synchronize: !syncFileEvents ? undefined : {
fileEvents: [
vscode.workspace.createFileSystemWatcher(filePattern),
Expand All @@ -124,7 +118,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<HlasmE

const extConfProvider = new HLASMExternalConfigurationProvider();
const extFiles = new HLASMExternalFiles(
externalFilesScheme,
schemeExternalFiles,
vscode.workspace.fs,
await getCacheInfo(vscode.Uri.joinPath(context.globalStorageUri, 'external.files.cache'), vscode.workspace.fs)
);
Expand Down Expand Up @@ -265,7 +259,7 @@ async function registerEditHelpers(context: vscode.ExtensionContext) {

context.subscriptions.push(vscode.commands.registerCommand('extension.hlasm-plugin.createCompleteConfig', ConfigurationsHandler.createCompleteConfig));

context.subscriptions.push(vscode.languages.registerCodeLensProvider({ language: 'hlasm' }, { provideCodeLenses: ConfigurationsHandler.provideCodeLenses }));
context.subscriptions.push(vscode.languages.registerCodeLensProvider({ language: languageIdHlasm }, { provideCodeLenses: ConfigurationsHandler.provideCodeLenses }));

// register continuation handlers
if (!((await vscode.commands.getCommands()).find(command => command == "extension.hlasm-plugin.insertContinuation" || command == "extension.hlasm-plugin.removeContinuation"))) {
Expand All @@ -292,13 +286,13 @@ async function registerDebugSupport(context: vscode.ExtensionContext, client: vs
context.subscriptions.push(vscode.commands.registerCommand('extension.hlasm-plugin.getCurrentProgramName', () => getCurrentProgramName()));

// register provider for all hlasm debug configurations
context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('hlasm', new HLASMConfigurationProvider()));
context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory('hlasm', new HLASMDebugAdapterFactory(client)));
context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider(debugTypeHlasm, new HLASMConfigurationProvider()));
context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory(debugTypeHlasm, new HLASMDebugAdapterFactory(client)));
}

async function registerExternalFileSupport(context: vscode.ExtensionContext, client: vscodelc.BaseLanguageClient, extFiles: HLASMExternalFiles) {
context.subscriptions.push(client.onDidChangeState(e => e.newState === vscodelc.State.Starting && extFiles.reset()));
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider(externalFilesScheme, extFiles.getTextDocumentContentProvider()));
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider(schemeExternalFiles, extFiles.getTextDocumentContentProvider()));
const datasetClient = HLASMExternalFilesFtp(context);
if (datasetClient)
extFiles.setClient('DATASET', datasetClient);
Expand All @@ -312,11 +306,11 @@ async function registerExternalFileSupport(context: vscode.ExtensionContext, cli
}

async function registerToContextWithClient(context: vscode.ExtensionContext, client: vscodelc.BaseLanguageClient, telemetry: Telemetry) {
context.subscriptions.push(vscode.languages.registerCodeActionsProvider(['hlasm', hlasmplugin_folder_filter, bridge_json_filter], new HLASMCodeActionsProvider(client)));
context.subscriptions.push(vscode.languages.registerCodeActionsProvider([languageIdHlasm, hlasmplugin_folder_filter, bridge_json_filter], new HLASMCodeActionsProvider(client)));

context.subscriptions.push(vscode.commands.registerCommand('extension.hlasm-plugin.toggleAdvisoryConfigurationDiagnostics', () => toggleAdvisoryConfigurationDiagnostics(client)));

context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider("hlasm", new HLASMVirtualFileContentProvider(client)));
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider(schemeVirtualFiles, new HLASMVirtualFileContentProvider(client)));

context.subscriptions.push(vscode.commands.registerCommand("extension.hlasm-plugin.downloadDependencies", (...args: any[]) => downloadDependencies(context, telemetry, client.outputChannel, ...args)));

Expand Down
7 changes: 4 additions & 3 deletions clients/vscode-hlasmplugin/src/hlasmLanguageDetection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import * as vscode from 'vscode';
import { ConfigurationsHandler } from './configurationsHandler'
import { getConfig } from './eventsHandler';
import { continuationColumn, languageIdHlasm } from './constants';

/**
* Runs automatic HLASM language detection on given files
Expand All @@ -39,11 +40,11 @@ export class HLASMLanguageDetection {
// check only plain text files
if (document.languageId == 'plaintext' && this.withoutExtension(document.uri)) {
if (this.checkHlasmLanguage(document)) {
vscode.languages.setTextDocumentLanguage(document, 'hlasm');
vscode.languages.setTextDocumentLanguage(document, languageIdHlasm);
return true;
}
}
return document.languageId == 'hlasm';
return document.languageId == languageIdHlasm;
}

//Checks for extension for plaintext files
Expand Down Expand Up @@ -80,7 +81,7 @@ export class HLASMLanguageDetection {
if ((this.referenceInstructions.test(line.toUpperCase()) || lastContinued) && line.length <= 80) {
score++;
// naive continuation check
lastContinued = line.length > 71 && ([...line][71] ?? ' ') !== ' ';
lastContinued = line.length > continuationColumn && ([...line][continuationColumn] ?? ' ') !== ' ';
}
}
});
Expand Down
10 changes: 4 additions & 6 deletions clients/vscode-hlasmplugin/src/hlasmListingServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
*/

import * as vscode from 'vscode';
import { EXTENSION_ID } from './extension';

const languageIdHlasmListing = 'hlasmListing';
import { EXTENSION_ID, initialBlanks, languageIdHlasmListing } from './constants';

const ordchar = /[A-Za-z0-9$#@_]/;

Expand Down Expand Up @@ -385,7 +383,7 @@ function isolateSymbol(l: Listing, document: vscode.TextDocument, position: vsco
const prevContinued = prevLine >= 0 && /[^ ]/.test(prevText[right]);
const thisContinued = /[^ ]/.test(thisText[right]);

const thisOffset = prevContinued ? 15 : 0;
const thisOffset = prevContinued ? initialBlanks : 0;

while (start > left + thisOffset && ordchar.test(thisText[start - 1]))
--start;
Expand All @@ -404,10 +402,10 @@ function isolateSymbol(l: Listing, document: vscode.TextDocument, position: vsco
prefix = prevText.substring(start, right);
}
if (thisContinued && end == right) {
end = left + 15;
end = left + initialBlanks;
while (end < right && ordchar.test(nextText[end]))
++end;
suffix = nextText.substring(left + 15, end);
suffix = nextText.substring(left + initialBlanks, end);
}

return (prefix + result + suffix).toUpperCase();
Expand Down
9 changes: 4 additions & 5 deletions clients/vscode-hlasmplugin/src/hlasmOutputContentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as vscodelc from 'vscode-languageclient';
import { uriFriendlyBase16Decode, uriFriendlyBase16Encode } from './conversions';
import { isCancellationError } from './helpers';
import { pickUser } from './uiUtils';
import { languageIdHlasm, schemeOutput } from './constants';

type OutputLine = {
level: number;
Expand All @@ -26,8 +27,6 @@ type OutputResult = OutputLine[];

type Options = { mnote: true, punch: true } | { mnote: false, punch: true } | { mnote: true, punch: false };

const scheme = 'hlasm-output';

function translateOptions(options: Options) {
let result = '';
if (options.mnote) result += 'M';
Expand All @@ -49,11 +48,11 @@ function createOutputUri(uri: vscode.Uri, options: Options) {
const query = uriFriendlyBase16Encode(uri.toString());
const path = `/${translateOptions(options)}/${uri.path.substring(uri.path.lastIndexOf('/') + 1)}.output`;

return vscode.Uri.from({ scheme, path, query });
return vscode.Uri.from({ scheme: schemeOutput, path, query });
}

export async function showOutputCommand(editor: vscode.TextEditor, _: vscode.TextEditorEdit, args: any) {
if (editor.document.languageId !== 'hlasm') return;
if (editor.document.languageId !== languageIdHlasm) return;
if (!args || args instanceof vscode.Uri)
args = await pickUser('Outputs to include:', [
{ label: 'MNOTE and PUNCH', value: { mnote: true, punch: true } },
Expand All @@ -80,7 +79,7 @@ export function registerOutputDocumentContentProvider(
}, disposables: vscode.Disposable[]) {

const changed = new vscode.EventEmitter<vscode.Uri>()
const provider = vscode.workspace.registerTextDocumentContentProvider(scheme, {
const provider = vscode.workspace.registerTextDocumentContentProvider(schemeOutput, {
onDidChange: changed.event,
async provideTextDocumentContent(uri: vscode.Uri, token: vscode.CancellationToken) {
const opts = extractOptions(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import * as vscode from 'vscode';
import * as vscodelc from 'vscode-languageclient';
import { schemeVirtualFiles } from './constants';

interface FileContent {
content: string;
Expand All @@ -25,7 +26,7 @@ export class HLASMVirtualFileContentProvider implements vscode.TextDocumentConte
return new Promise((resolve, reject) => {
const trimmed = uri.authority.trim();
const file_id = +trimmed;
if (uri.scheme === 'hlasm' && trimmed.length > 0 && !isNaN(file_id))
if (uri.scheme === schemeVirtualFiles && trimmed.length > 0 && !isNaN(file_id))
this.client.sendRequest<FileContent>("get_virtual_file_content", { id: file_id }, token)
.then(c => resolve(c.content))
.catch(e => reject(e));
Expand Down
2 changes: 1 addition & 1 deletion clients/vscode-hlasmplugin/src/serverFactory.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

import * as vscode from 'vscode';
import * as vscodelc from 'vscode-languageclient/browser';
import { EXTENSION_ID } from './extension';
import { getConfig } from './eventsHandler';
import { ServerVariant, decorateArgs } from './serverFactory.common';
import { EXTENSION_ID } from './constants';

function worker_main(extensionUri: string, hlasm_arguments: string[]) {

Expand Down
Loading

0 comments on commit dc08a9b

Please sign in to comment.