diff options
Diffstat (limited to 'plugin/src')
| -rw-r--r-- | plugin/src/extension.js | 31 | ||||
| -rw-r--r-- | plugin/src/indentation.js | 58 | ||||
| -rw-r--r-- | plugin/src/lexical.js | 33 |
3 files changed, 120 insertions, 2 deletions
diff --git a/plugin/src/extension.js b/plugin/src/extension.js index 1101ed6..a7b4834 100644 --- a/plugin/src/extension.js +++ b/plugin/src/extension.js @@ -1,6 +1,33 @@ 'use strict'; -// Language registration and TextMate highlighting are declarative. -function activate() {} +const vscode = require('vscode'); +const { indentationEdits } = require('./indentation'); +const { codeOnly } = require('./lexical'); +const languageId = 'cie-pseudocode'; + +function formatLines(document, startLine, endLine, keepBlankIndent = false) { + const lines = document.getText().split(/\r?\n/); + return indentationEdits(lines, startLine, endLine, keepBlankIndent).map(edit => + vscode.TextEdit.replace(new vscode.Range(edit.line, 0, edit.line, edit.length), edit.text)); +} + +function activate(context) { + context.subscriptions.push( + vscode.languages.registerDocumentRangeFormattingEditProvider(languageId, { + provideDocumentRangeFormattingEdits(document, range) { + const endLine = range.end.character === 0 && range.end.line > range.start.line + ? range.end.line - 1 : range.end.line; + return formatLines(document, range.start.line, endLine); + } + }), + vscode.languages.registerOnTypeFormattingEditProvider(languageId, { + provideOnTypeFormattingEdits(document, position, char) { + if (char !== '\n' && !/^\s*(?:ELSE|ENDIF|ENDWHILE|UNTIL|NEXT|ENDFUNCTION|ENDPROCEDURE|ENDTYPE)\b/.test( + codeOnly(document.lineAt(position.line).text))) return []; + return formatLines(document, position.line, position.line, char === '\n'); + } + }, '\n', 'E', 'F', 'L', 'T', 'N') + ); +} module.exports = { activate }; diff --git a/plugin/src/indentation.js b/plugin/src/indentation.js new file mode 100644 index 0000000..c8af770 --- /dev/null +++ b/plugin/src/indentation.js @@ -0,0 +1,58 @@ +'use strict'; + +const { codeOnly } = require('./lexical'); +const closers = { + ENDIF: 'IF', ENDWHILE: 'WHILE', UNTIL: 'REPEAT', NEXT: 'FOR', + ENDFUNCTION: 'FUNCTION', ENDPROCEDURE: 'PROCEDURE', ENDTYPE: 'TYPE' +}; + +function lineDepths(lines) { + const stack = []; + const depth = () => stack.filter(block => block.active).length; + return lines.map(line => { + const code = codeOnly(line).trim(); + const keyword = /^([A-Za-z][A-Za-z0-9_]*)\b/.exec(code)?.[1]; + if (closers[keyword] || keyword === 'ELSE') { + const kind = keyword === 'ELSE' ? 'IF' : closers[keyword]; + const index = stack.findLastIndex(block => block.kind === kind); + if (index >= 0) { + stack.length = index; + const current = depth(); + if (keyword === 'ELSE') stack.push({ kind: 'IF', active: true }); + return current; + } + // Incomplete code should still format without negative indentation. + return depth(); + } + + const current = depth(); + if (keyword === 'THEN' || keyword === 'DO') { + const top = stack[stack.length - 1]; + if (top?.kind === (keyword === 'THEN' ? 'IF' : 'WHILE')) top.active = true; + } else if (keyword === 'IF' || keyword === 'WHILE') { + stack.push({ kind: keyword, active: new RegExp( + keyword === 'IF' ? '\\bTHEN\\b' : '\\bDO\\b').test(code) }); + } else if (['FOR', 'REPEAT', 'FUNCTION', 'PROCEDURE'].includes(keyword)) { + stack.push({ kind: keyword, active: true }); + } else if (/^TYPE\s+[A-Za-z][A-Za-z0-9_]*$/.test(code)) { + stack.push({ kind: 'TYPE', active: true }); + } + return current; + }); +} + +function indentationEdits(lines, startLine, endLine, keepBlankIndent = false) { + const depths = lineDepths(lines.slice(0, endLine + 1)); + const edits = []; + for (let line = startLine; line <= endLine; line++) { + const oldIndent = /^[\t ]*/.exec(lines[line])[0]; + const blank = lines[line].trim() === ''; + const newIndent = blank && !keepBlankIndent ? '' : '\t'.repeat(depths[line]); + if (oldIndent !== newIndent) { + edits.push({ line, length: oldIndent.length, text: newIndent }); + } + } + return edits; +} + +module.exports = { lineDepths, indentationEdits }; diff --git a/plugin/src/lexical.js b/plugin/src/lexical.js new file mode 100644 index 0000000..3fc856a --- /dev/null +++ b/plugin/src/lexical.js @@ -0,0 +1,33 @@ +'use strict'; + +// Preserve UTF-16 offsets and newlines, masking CIE quotes and // comments. +// Unlike C++, CIE has no escapes or multiline literals. +function codeOnly(text) { + let quote = null; + let comment = false; + const result = []; + for (let i = 0; i < text.length; i++) { + const char = text[i]; + if (char === '\n' || char === '\r') { + quote = null; + comment = false; + result.push(char); + } else if (comment) { + result.push(' '); + } else if (quote) { + if (char === quote) quote = null; + result.push(' '); + } else if (char === '"' || char === "'") { + quote = char; + result.push(' '); + } else if (char === '/' && text[i + 1] === '/') { + comment = true; + result.push(' '); + } else { + result.push(char); + } + } + return result.join(''); +} + +module.exports = { codeOnly }; |
