Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarlevin committed Jul 6, 2024
1 parent 6462d0f commit 0258bdb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
"markdownDescription": "Select which method to use when viewing the output of a pretext project. Leaving this at the default will ask for available methods each time. Your choice will likely depend on the size of your project and whether you are building output other than the 'web' target."
},
"pretext-tools.formatter.blankLines": {
"type":"string",
"type": "string",
"default": "some",
"enum": [
"few",
Expand Down Expand Up @@ -304,4 +304,4 @@
"prettier": {
"tabWidth": 2
}
}
}
1 change: 0 additions & 1 deletion src/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ function getCurrentTag(
return currentTag;
}


// Types for snippets
type Snippet = {
prefix: string;
Expand Down
1 change: 0 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ export async function activate(context: vscode.ExtensionContext) {
"log"
);

// set up status bar item
// set up status bar item
ptxSBItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left,
Expand Down
36 changes: 29 additions & 7 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,17 @@ const math_display = ["me", "men", "md", "mdn"];

const footnote_like = ["fn"];

const nestable_tags = ["ul", "ol", "li", "p", "task", "figure", "sidebyside", "notation", "row"];
const nestable_tags = [
"ul",
"ol",
"li",
"p",
"task",
"figure",
"sidebyside",
"notation",
"row",
];

// note that c is special, because it is inline verbatim
const verbatimTags = [
Expand Down Expand Up @@ -249,7 +259,10 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] {
for (let btag of blockTags) {
if (allText.includes("<" + btag)) {
// start tag can be <tag>, <tag attr="val">, or <tag xmlns="..."> but shouldn't be self closing (no self closing tag would have xmlns in it)
let startTag = new RegExp("<" + btag + "(>|([^/]*?)>|(.*xmlns.*?)>)", "g");
let startTag = new RegExp(
"<" + btag + "(>|([^/]*?)>|(.*xmlns.*?)>)",
"g"
);
let endTag = new RegExp("<\\/" + btag + ">(.?)", "g");
allText = allText.replace(startTag, "\n$&\n");
allText = allText.replace(endTag, "\n$&\n");
Expand All @@ -259,7 +272,7 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] {
let startTag = new RegExp("<" + tag + "(.*?)>", "g");
let endTag = new RegExp("<\\/" + tag + ">(.?)", "g");
let selfCloseTag = new RegExp("<" + tag + "(.*?)/>", "g");
allText = allText.replace(startTag, "\n$&");
allText = allText.replace(startTag, "\n$&");
allText = allText.replace(endTag, "$&\n");
allText = allText.replace(selfCloseTag, "$&\n");
}
Expand Down Expand Up @@ -293,7 +306,7 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] {
continue;
} else if (trimmedLine.startsWith("<?")) {
// It's the start line of the file:
fixedLines.push(trimmedLine+"\n");
fixedLines.push(trimmedLine + "\n");
} else if (trimmedLine.startsWith("<!--")) {
// It's a comment:
fixedLines.push(indentChar.repeat(level) + trimmedLine);
Expand All @@ -318,13 +331,18 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] {
fixedLines.push(line);
} else {
if (breakSentences) {
trimmedLine = trimmedLine.replace(/\.\s+/g, ".\n" + indentChar.repeat(level));
trimmedLine = trimmedLine.replace(
/\.\s+/g,
".\n" + indentChar.repeat(level)
);
}
fixedLines.push(indentChar.repeat(level) + trimmedLine);
}
}
// Second pass: add empty line between appropriate tags depending on blankLines setting.
const blankLines = vscode.workspace.getConfiguration("pretext-tools").get("formatter.blankLines");
const blankLines = vscode.workspace
.getConfiguration("pretext-tools")
.get("formatter.blankLines");
switch (blankLines) {
case "few":
// do nothing
Expand All @@ -345,7 +363,11 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] {
break;
case "many":
for (let i = 0; i < fixedLines.length - 1; i++) {
if (fixedLines[i].trim().startsWith("</") || (fixedLines[i].trim().startsWith("<") && fixedLines[i+1].trim().startsWith("<"))) {
if (
fixedLines[i].trim().startsWith("</") ||
(fixedLines[i].trim().startsWith("<") &&
fixedLines[i + 1].trim().startsWith("<"))
) {
fixedLines[i] += "\n";
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/latextopretext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function makeLines(text: string) {
if (
lines[i].length > 0 &&
lines[i + 1].length > 0 &&
(lines[i+1].startsWith("\\[") ||
(lines[i + 1].startsWith("\\[") ||
lines[i + 1].startsWith("\\]") ||
(!lines[i].startsWith("\\") && !lines[i + 1].startsWith("\\")))
) {
Expand Down

0 comments on commit 0258bdb

Please sign in to comment.