Skip to content

Commit

Permalink
indent on format respects editor options
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarlevin committed Jul 6, 2024
1 parent ff38892 commit bf3aead
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Bugs with formatting.
- Formatting now respects editor's choice of tab/space for indentation.
- Conversion of selection: convert reserved characters (&, <, >) to entities.


Expand Down
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@
},
"dependencies": {
"@vscode/vsce": "^2.22.0",
"date-and-time": "^3.3.0",
"fs.promises.exists": "^1.1.3",
"moment": "^2.29.1",
"moment": "^2.30.1",
"unified": "^10.1.2",
"unist-util-visit": "^4.1.0",
"xast-util-from-xml": "^2.0.1",
Expand Down
26 changes: 19 additions & 7 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,18 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] {
.getConfiguration("pretext-tools")
.get("formatter.breakSentences");
console.log("extraLineBreaks is", extraLineBreaks);

// Determine the number of spaces or tabs each indent is in current editor.
let editorTabSize = vscode.window.activeTextEditor?.options.tabSize;
console.log("editorTabSize is", editorTabSize);
let editorInsertSpaces = vscode.window.activeTextEditor?.options.insertSpaces;
console.log("editorInsertSpaces is", editorInsertSpaces);
// Set indent character to \t or a number of ss based on editor settings.
let indentChar = "\t";
if (editorInsertSpaces && typeof editorTabSize === "number") {
indentChar = " ".repeat(editorTabSize);
}

let level = 0;
let verbatim = false;
let lines = allText.split(/\r\n|\r|\n/g);
Expand All @@ -284,19 +296,19 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] {
fixedLines.push(trimmedLine+"\n");
} else if (trimmedLine.startsWith("<!--")) {
// It's a comment:
fixedLines.push("\t".repeat(level) + trimmedLine);
fixedLines.push(indentChar.repeat(level) + trimmedLine);
} else if (closeTagMatch) {
if (blockTags.includes(closeTagMatch[1])) {
level = Math.max(0, level - 1);
fixedLines.push("\t".repeat(level) + trimmedLine);
fixedLines.push(indentChar.repeat(level) + trimmedLine);
} else if (verbatimTags.includes(closeTagMatch[1])) {
verbatim = false;
fixedLines.push("\t".repeat(level) + trimmedLine);
fixedLines.push(indentChar.repeat(level) + trimmedLine);
} else {
fixedLines.push("\t".repeat(level) + trimmedLine);
fixedLines.push(indentChar.repeat(level) + trimmedLine);
}
} else if (openTagMatch) {
fixedLines.push("\t".repeat(level) + trimmedLine);
fixedLines.push(indentChar.repeat(level) + trimmedLine);
if (blockTags.includes(openTagMatch[1])) {
level += 1;
} else if (verbatimTags.includes(openTagMatch[1])) {
Expand All @@ -306,9 +318,9 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] {
fixedLines.push(line);
} else {
if (extraLineBreaks) {
trimmedLine = trimmedLine.replace(/\.\s+/g, ".\n" + "\t".repeat(level));
trimmedLine = trimmedLine.replace(/\.\s+/g, ".\n" + indentChar.repeat(level));
}
fixedLines.push("\t".repeat(level) + trimmedLine);
fixedLines.push(indentChar.repeat(level) + trimmedLine);
}
}
// Second pass: add empty line between appropriate tags.
Expand Down
5 changes: 3 additions & 2 deletions src/importFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from "path";
import * as vscode from "vscode";
import * as fs from "fs";
import * as https from "https";
import moment = require("moment");
import * as date from "date-and-time";

const pretextWriter = path.join(homedir(), ".ptx", "pandoc", "pretext.lua");

Expand Down Expand Up @@ -44,11 +44,12 @@ export function convertToPretext(method: string = "pandoc") {
function runPlastex(inputfile: string) {
console.log("Converting to pretext using PlasTeX converter");
// set outputDir to imports/timestamp
const now = new Date();
const outputDir = path.join(
path.dirname(inputfile),
"imports",
path.basename(inputfile, ".tex"),
moment().format("YYYYMMDD-HHmm")
date.format(now, "YYYYMMDD-HHmm")
);
console.log("outputDir: " + outputDir);
// run pretext import
Expand Down

0 comments on commit bf3aead

Please sign in to comment.