diff --git a/CHANGELOG.md b/CHANGELOG.md index ab77a3d..bbe8dc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/package-lock.json b/package-lock.json index d55d973..3f29b45 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,9 @@ "license": "See license in LICENSE", "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", @@ -1277,6 +1278,11 @@ "url": "https://github.com/sponsors/fb55" } }, + "node_modules/date-and-time": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/date-and-time/-/date-and-time-3.3.0.tgz", + "integrity": "sha512-UguWfh9LkUecVrGSE0B7SpAnGRMPATmpwSoSij24/lDnwET3A641abfDBD/TdL0T+E04f8NWlbMkD9BscVvIZg==" + }, "node_modules/debug": { "version": "4.3.5", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.5.tgz", diff --git a/package.json b/package.json index eea907f..266cd11 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/formatter.ts b/src/formatter.ts index 22d3de0..b445633 100644 --- a/src/formatter.ts +++ b/src/formatter.ts @@ -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); @@ -284,19 +296,19 @@ export function formatPTX(document: vscode.TextDocument): vscode.TextEdit[] { fixedLines.push(trimmedLine+"\n"); } else if (trimmedLine.startsWith("