Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/surround with monitor action #223

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions client/src/editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { ExtensionContext, Range, SnippetString, TextDocument, commands, window } from "vscode";

export function initialise(context: ExtensionContext) {
context.subscriptions.push(
commands.registerCommand("vscode-rpgle.surroundWithMonitor", () => surroundWithSnippet(lines => [
"",
"monitor;",
...lines,
"on-excp '$1';",
"on-error$2;",
"endmon;",
""
]))
);
}

function surroundWithSnippet(snippetProvider: (lines: string[]) => string[]) {
const editor = window.activeTextEditor;
if (editor) {
const document = editor.document;
let line = editor.selection.start.line;
if (isFreeRPGLE(document, line)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is interesting, checking if the logic is currently free format. Perhaps instead if the document is not **free, substring the first 8(?) characters and then surround that? That might be nicer than checking for /free and /end-free.

// First let's find out if this line starts elsewhere
while ((line - 1) >= 0 && !document.lineAt(line).text.trim().endsWith(`;`) && line-- > 0);
const startLine = line;

// Now get all the relevant lines
const lines: string[] = [];
let initialIdentation = document.lineAt(line).firstNonWhitespaceCharacterIndex;
while (line < document.lineCount) {
const currentLine = document.lineAt(line++);
const text = currentLine.text;
//Keep identation
const indent = currentLine.firstNonWhitespaceCharacterIndex - initialIdentation;
lines.push(`${"".padEnd(2 + (indent > 0 ? indent : 0))}${text.trim()}`);
const maybeComment = text.lastIndexOf("//");
if (line > editor.selection.end.line && (maybeComment > -1 ? text.substring(maybeComment + 1) : text).endsWith(';')) {
break;
}
}

editor.insertSnippet(new SnippetString(snippetProvider(lines).join("\n")),
new Range(startLine, document.lineAt(startLine).firstNonWhitespaceCharacterIndex, line - 1, 9999));
}
else {
window.showWarningMessage("Selection is not valid Free RPGLE code");
}
}
}

function isFreeRPGLE(document: TextDocument, currentLine: number) {
if (document.lineAt(0).text.substring(0, 6).toLowerCase() === `**free`) {
return true;
}
else {
for (let i = currentLine; i < document.lineCount; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this check. In modern versions of RPGLE (7.3+), /free and /end-free are optional, so if they're not used, then it might always return false by the looks of it.

const line = document.lineAt(i).text;
if (line.length > 10 && line.substring(6, 11).toLowerCase() === '/free') {
return false;
}
else if (line.length > 14 && line.substring(6, 15).toLowerCase() === '/end-free') {
return true;
}
}
}
}
7 changes: 4 additions & 3 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* ------------------------------------------------------------------------------------------ */

import * as path from 'path';
import { workspace, ExtensionContext, Uri, commands, RelativePattern } from 'vscode';
import { ExtensionContext, workspace } from 'vscode';

import * as Linter from "./linter";
import * as columnAssist from "./columnAssist";
import * as Linter from "./linter";


import {
Expand All @@ -18,6 +18,7 @@ import {
} from 'vscode-languageclient/node';

import { projectFilesGlob } from './configuration';
import { initialise as initialiseEditorCommands } from './editor';
import buildRequestHandlers from './requests';

let client: LanguageClient;
Expand Down Expand Up @@ -74,7 +75,7 @@ export function activate(context: ExtensionContext) {

Linter.initialise(context);
columnAssist.registerColumnAssist(context);

initialiseEditorCommands(context);
// context.subscriptions.push(...initBuilder(client));

console.log(`started`);
Expand Down
37 changes: 35 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
"vscode": "^1.70.0"
},
"keywords": [
"rpg", "rpgle", "ibmi", "iseries", "as400"
"rpg",
"rpgle",
"ibmi",
"iseries",
"as400"
],
"categories": [
"Programming Languages",
Expand Down Expand Up @@ -58,15 +62,44 @@
"command": "vscode-rpgle.openLintConfig",
"title": "Open RPGLE lint configuration",
"category": "RPGLE"
},
{
"command": "vscode-rpgle.surroundWithMonitor",
"title": "Surround with monitor",
"category": "RPGLE"
}
],
"submenus": [
{
"id": "vscode-rpgle.editor.menu",
"label": "RPGLE"
}
],
"menus": {
"vscode-rpgle.editor.menu": [
{
"command": "vscode-rpgle.surroundWithMonitor"
}
],
"commandPalette": [
{
"command": "vscode-rpgle.surroundWithMonitor",
"when": "editorLangId === rpgle"
}
],
"view/item/context": [
{
"command": "vscode-rpgle.openLintConfig",
"when": "view == objectBrowser && viewItem == filter",
"group": "1_LibActions@2"
}
],
"editor/context": [
{
"submenu": "vscode-rpgle.editor.menu",
"when": "editorLangId === rpgle",
"group": "01_rpgleActions@01"
}
]
}
},
Expand Down Expand Up @@ -103,4 +136,4 @@
"webpack": "^5.24.3",
"webpack-cli": "^4.5.0"
}
}
}