Skip to content

Commit

Permalink
Fix number of newlines allowed before func/class (tests are failing)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaelonSuzuka committed Mar 8, 2024
1 parent f8b27d7 commit d70164f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@
"default": true,
"description": "Whether to reveal the terminal when launching the Godot Editor"
},
"godotTools.formatter.emptyLinesBeforeFunctions": {
"type": "string",
"enum": [
"one",
"two"
],
"enumDescriptions": [
"One line before functions. A more compact style.",
"Two lines before functions. Conforms to the official GDScript style guide."
],
"default": "two",
"description": "Number of blank lines to leave before functions."
},
"godotTools.lsp.serverProtocol": {
"type": [
"string"
Expand Down
37 changes: 25 additions & 12 deletions src/formatter/textmate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from "fs";
import * as vsctm from "vscode-textmate";
import * as oniguruma from "vscode-oniguruma";
import { keywords, symbols } from "./symbols";
import { get_extension_uri, createLogger } from "../utils";
import { get_configuration, get_extension_uri, createLogger } from "../utils";

const log = createLogger("formatter.tm");

Expand Down Expand Up @@ -170,8 +170,11 @@ export function format_document(document: TextDocument): TextEdit[] {
}
const edits: TextEdit[] = [];

const emptyLinesBetweenFunctions : "one" | "two" = get_configuration("formatter.emptyLinesBetweenFunctions");

let lineTokens: vsctm.ITokenizeLineResult = null;
let onlyEmptyLinesSoFar = true;
let emptyLineCount = 0;
for (let lineNum = 0; lineNum < document.lineCount; lineNum++) {
const line = document.lineAt(lineNum);

Expand All @@ -181,22 +184,32 @@ export function format_document(document: TextDocument): TextEdit[] {
if (onlyEmptyLinesSoFar) {
edits.push(TextEdit.delete(line.rangeIncludingLineBreak));
} else {
// Limit the number of consecutive empty lines
const maxEmptyLines: number = 1;
if (maxEmptyLines === 1) {
if (lineNum < document.lineCount - 1 && document.lineAt(lineNum + 1).isEmptyOrWhitespace) {
edits.push(TextEdit.delete(line.rangeIncludingLineBreak));
}
} else if (maxEmptyLines === 2) {
if (lineNum < document.lineCount - 2 && document.lineAt(lineNum + 1).isEmptyOrWhitespace && document.lineAt(lineNum + 2).isEmptyOrWhitespace) {
edits.push(TextEdit.delete(line.rangeIncludingLineBreak));
}
}
emptyLineCount++;
}
continue;
}
onlyEmptyLinesSoFar = false;

// delete consecutive empty lines
if (emptyLineCount) {
let maxEmptyLines = 1;

const start = line.text.trimStart();
if (emptyLinesBetweenFunctions === "two") {
if (start.startsWith("func")) {
maxEmptyLines++;
}
}
if (start.startsWith("class")) {
maxEmptyLines++;
}

for (let i = (emptyLineCount - maxEmptyLines); i; i--) {
edits.push(TextEdit.delete(document.lineAt(lineNum - i).rangeIncludingLineBreak));
}
emptyLineCount = 0;
}

// skip comments
if (line.text[line.firstNonWhitespaceCharacterIndex] === "#") {
continue;
Expand Down

0 comments on commit d70164f

Please sign in to comment.