Skip to content

Commit

Permalink
Respect editorconfig indent size and eol
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Nov 30, 2023
1 parent 92d0e20 commit 02e0228
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 20 deletions.
112 changes: 98 additions & 14 deletions 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 @@ -285,7 +285,8 @@
"pokey.parse-tree"
],
"dependencies": {
"ignore": "^5.2.4"
"ignore": "^5.2.4",
"editorconfig": "^2.0.0"
},
"devDependencies": {
"@types/glob": "^8.1.0",
Expand Down
4 changes: 1 addition & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { registerCommands } from "./commands/registerCommands";
import { registerLanguageCodeActions } from "./language/registerLanguageCodeActions";
import { registerLanguageDefinitions } from "./language/registerLanguageDefinitions";
import { registerLanguageFormatter } from "./language/registerLanguageFormatter";
import { registerStateUpdater } from "./state";
import { createTabView } from "./tabView";
import {
getCommandServerExtension,
Expand Down Expand Up @@ -34,7 +33,6 @@ async function activateExtension(context: vscode.ExtensionContext): Promise<void
registerLanguageDefinitions(),
registerLanguageCodeActions(parseTreeExtension),
registerLanguageFormatter(parseTreeExtension),
createTabView(),
isTesting ? vscode.Disposable.from() : registerStateUpdater()
createTabView()
);
}
1 change: 1 addition & 0 deletions src/language/TreeSitterFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class TreeSitterFormatter implements LanguageFormatterTree {
return ` ${node.text}`;

case "#":
case "_":
case "predicate_type":
case "identifier":
case "quantifier":
Expand Down
39 changes: 37 additions & 2 deletions src/language/registerLanguageFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as editorconfig from "editorconfig";
import {
Disposable,
EndOfLine,
Expand Down Expand Up @@ -55,8 +56,42 @@ function provideDocumentFormattingEditsForText(
}

function parseOptions(document: TextDocument, options: FormattingOptions): [string, string] {
const indentation = options.insertSpaces ? new Array(options.tabSize).fill(" ").join("") : "\t";
const eol = document.eol === EndOfLine.LF ? "\n" : "\r\n";
const config = editorconfig.parseSync(document.uri.fsPath);

const insertSpaces = (() => {
switch (config.indent_style) {
case "space":
return true;
case "tab":
return false;
default:
return options.insertSpaces;
}
})();

const tabSize = (() => {
if (typeof config.indent_size === "number") {
return config.indent_size;
}
if (config.indent_size === "tab" && typeof config.tab_width === "number") {
return config.tab_width;
}
return options.tabSize;
})();

const indentation = insertSpaces ? new Array(tabSize).fill(" ").join("") : "\t";

const eol = (() => {
switch (config.end_of_line) {
case "lf":
return "\n";
case "crlf":
return "\r\n";
default:
return document.eol === EndOfLine.LF ? "\n" : "\r\n";
}
})();

return [indentation, eol];
}

Expand Down

0 comments on commit 02e0228

Please sign in to comment.