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

Add fixed-format prompter back #317

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
124 changes: 122 additions & 2 deletions extension/client/src/columnAssist.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { DecorationOptions, ExtensionContext, Range, window } from 'vscode';
import { commands, DecorationOptions, ExtensionContext, Range, window } from 'vscode';
import * as Configuration from "./configuration";
import { loadBase } from './base';

const currentArea = window.createTextEditorDecorationType({
backgroundColor: `rgba(242, 242, 109, 0.3)`,
Expand All @@ -12,7 +13,8 @@ const notCurrentArea = window.createTextEditorDecorationType({
border: `1px solid grey`,
});

const specs = require(`./schemas/specs`);
import { SpecFieldDef, SpecFieldValue, specs } from './schemas/specs';
import type { Field } from '@halcyontech/vscode-ibmi-types/api/CustomUI';

const getAreasForLine = (line: string, index: number) => {
if (line.length < 6) return undefined;
Expand All @@ -33,6 +35,33 @@ const getAreasForLine = (line: string, index: number) => {

export function registerColumnAssist(context: ExtensionContext) {
context.subscriptions.push(
commands.registerCommand(`vscode-rpgle.rpgleColumnAssistant`, async () => {
const editor = window.activeTextEditor;
if (editor) {
const document = editor.document;

if (document.languageId === `rpgle`) {
if (document.getText(new Range(0, 0, 0, 6)).toUpperCase() !== `**FREE`) {
const lineNumber = editor.selection.start.line;
const positionIndex = editor.selection.start.character;

const positionsData = await promptLine(
document.getText(new Range(lineNumber, 0, lineNumber, 100)),
positionIndex
);

if (positionsData) {
window.showTextDocument(document).then(newEditor => {
newEditor.edit(editBuilder => {
editBuilder.replace(new Range(lineNumber, 0, lineNumber, 80), positionsData);
});
})
}
}
}
}
}),

window.onDidChangeTextEditorSelection(e => {
if (Configuration.get(`showFixedFormatOutline`)) {
const editor = e.textEditor;
Expand Down Expand Up @@ -78,3 +107,94 @@ export function registerColumnAssist(context: ExtensionContext) {
}),
)
}

interface FieldBox {
id: string,
text: string,
content: string
values?: SpecFieldValue[],
maxLength?: number
}

async function promptLine (line: string, _index: number): Promise<string|undefined> {
const base = loadBase();

if (!base) {
window.showErrorMessage(`Code for IBM i is not installed. It is required due to required UI tools.`);
return undefined;
};


if (line.length < 6) return undefined;
if (line[6] === `*`) return undefined;
line = line.padEnd(80);

const specLetter = line[5].toUpperCase();
if (specs[specLetter]) {
const specification = specs[specLetter];

let parts: FieldBox[] = [];

specification.forEach(box => {
parts.push({
id: box.id,
text: box.name,
content: line.substring(box.start, box.end+1).trimEnd(),
values: box.values,
maxLength: box.values ? undefined : (box.end+1)-box.start
});
});

const ui = base.customUI();

parts.forEach((box, index) => {
if (box.values) {
//Select box
ui.addSelect(box.id, box.text, box.values.map(item => ({
selected: item.value.toUpperCase() === box.content.toUpperCase(),
value: item.value,
description: item.value,
text: item.text
})))

} else {
//Input field
ui.addInput(box.id, box.text);
ui.fields[index].default = box.content;
ui.fields[index].maxlength = box.maxLength;
}
});

ui.addButtons(
{ id: `apply`, label: `Apply changes` },
{ id: `cancel`, label: `Cancel` }
);

const result = await ui.loadPage<{[key: string]: string}>(`Column Assistant`);

if (result && result.data) {
result.panel.dispose();
const data = result.data;

if (data.buttons !== `cancel`) {
let spot: SpecFieldDef|undefined, length: number;
for (const key in data) {
spot = specification.find(box => box.id === key);
if (spot) {
length = (spot.end+1)-spot.start;

if (data[key].length > length) data[key] = data[key].substr(0, length);

line = line.substring(0, spot.start) + (spot.padStart ? data[key].padStart(length) : data[key].padEnd(length)) + line.substring(spot.end+1);
}
}

return line.trimEnd();
}
}

return undefined;
} else {
return undefined;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
export type SpecFieldValue = {value: string, text: string};
export type SpecFieldDef = {id: string, name: string, start: number, end: number, values?: SpecFieldValue[], padStart?: boolean}

export const specs: {[spec: string]: SpecFieldDef[]} = {
C: [
{
id: `controlLevel`,
Expand Down
Loading