Skip to content

Commit

Permalink
get formatting working
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarlevin committed Dec 12, 2023
1 parent 617b976 commit 3961d79
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [UNRELEASED]

### Added

- Formatting support using vs-code's built-in formatting engine. From the command palette, select "Format Document" or use the keyboard shortcut `CTRL+SHIFT+I`.

### Fixed

- Bug that prevent initialization if not in a pretext project folder.


## [0.13.0] - 2023-11-27

### Added
Expand Down
19 changes: 11 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,16 @@ function getTargets() {
.split(/\r?\n/)
.filter(Boolean);
// Set up dictionary for quickselect:
let targetSelection = [];
for (let target of targets) {
targetSelection.push({
label: target,
description: "Build source as " + target,
});
}
return targetSelection;
if (targets.length > 0) {
let targetSelection = [];
for (let target of targets) {
targetSelection.push({
label: target,
description: "Build source as " + target,
});
}
return targetSelection;
} else {return [{label: "No PreTeXt project found.", description: "Change to directory containing a project.ptx file."}];}
} catch (err) {
console.log("getTargets() Error: \n", err);
return [];
Expand Down Expand Up @@ -442,6 +444,7 @@ export function activate(context: vscode.ExtensionContext) {
console.log("Pretext is installed is:", ptxInstalled);

var targetSelection = getTargets();
console.log("targetSelection is:", targetSelection);
lastTarget = targetSelection[0].label;
pretextCommandList[0].label = "Build " + lastTarget;
console.log(
Expand Down
33 changes: 24 additions & 9 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const docEnvs = ['proof',
'activity', 'remark', 'warning', 'table', 'tabular',
'list', 'listing', 'program', 'console', 'demonstration',
'images', 'image',
'fact', 'subtask', 'notation', 'claim', 'biblio',
'fact', 'subtask', 'claim', 'biblio',
'poem', 'stanza'];

const docPieces = ['title', 'subtitle', 'cell', 'caption',
Expand Down Expand Up @@ -62,7 +62,9 @@ const verbatimTags = ['latex-image-preamble', 'latex-image', 'latex-preamble',
'tikzpicture', 'tikz', 'code',
'c'];

const blockTags = docStructure.concat(docSecs).concat(docEnvs).concat(nestable_tags);
const newlineTags = docStructure.concat(docSecs).concat(docEnvs).concat(nestable_tags);

const blockTags = newlineTags.concat(math_display);



Expand All @@ -78,21 +80,24 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] {
// First clean up document so that each line is a single tag when appropriate.
let allText = document.getText();

console.log("Getting ready to start formatting.")
for (let btag of blockTags) {
let startTag = new RegExp('<'+btag+'(.*?)>', 'g');
let endTag = new RegExp('<\\/'+btag+'>', 'g');
let startTag = new RegExp('<'+btag+'(>|(\s[^/]*?)>)', 'g');
let endTag = new RegExp('<\\/'+btag+'>(.?)', 'g');
allText = allText.replace(startTag, "\n$&\n");
allText = allText.replace(endTag, "\n$&\n");
}

let level = 0;
let verbatim = false;
let lines = allText.split(/\r\n|\r|\n/g);
console.log("Finished splitting lines. Now will process", lines.length, "lines.");
let fixedLines = [];
for (let line of lines) {
console.log("level is", level, "and verbatim is", verbatim, "and line is", line);
let trimmedLine = line.trim();
let openTagMatch = /^<(\w*?)(\s.*?|>)$/.exec(trimmedLine);
let closeTagMatch = /^<\/(\w*?)(\s.*?|>)$/.exec(trimmedLine);
let closeTagMatch = /^<\/(\w*?)(\s.*?|>)(.?)$/.exec(trimmedLine);
let selfCloseTagMatch = /^<(\w*?)(\s.*?\/>|\/>)$/.exec(trimmedLine);
if (trimmedLine.length === 0) {
continue;
Expand All @@ -104,7 +109,7 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] {
fixedLines.push("\t".repeat(level) + trimmedLine);
} else if (closeTagMatch) {
if (blockTags.includes(closeTagMatch[1])) {
level -= 1;
level = Math.max(0, level-1);
fixedLines.push("\t".repeat(level) + trimmedLine);
} else if (verbatimTags.includes(closeTagMatch[1])) {
verbatim = false;
Expand All @@ -126,12 +131,22 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] {
}
// Second pass: add empty line between appropriate tags.
for (let i = 0; i < fixedLines.length-1; i++) {
if (/<\/p>/.test(fixedLines[i]) && /<p>/.test(fixedLines[i+1])) {
fixedLines[i] += "\n";
if (fixedLines[i].trim().startsWith("</")){
for (let tag of newlineTags) {
let startTag = new RegExp('<'+tag+'(.*?)>', 'g');
if (startTag.test(fixedLines[i+1])) {
console.log("Adding newline between", fixedLines[i], "and", fixedLines[i+1]);
fixedLines[i] += "\n";
}
}
}
}
allText = fixedLines.join("\n");
// Add document identifier line if missing:
if (!fixedLines[0].trim().startsWith('<?xml')) {
fixedLines.unshift('<?xml version="1.0" encoding="UTF-8" ?>');
}

allText = fixedLines.join("\n");

return [vscode.TextEdit.replace(document.validateRange(new vscode.Range(0, 0, document.lineCount, 0)), allText)];
}

0 comments on commit 3961d79

Please sign in to comment.