Skip to content

Commit

Permalink
added indentOffset to support snippets with non-zero indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe committed Nov 30, 2024
1 parent 3f0103f commit cad782a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## dev branch / next version (1.x.x)

- added indentOffset to support code snippets with non-zero indentation levels

## version 1.17.1 (2024-10-22)

- Fixed WhitespacePolicy compilation error
Expand Down
22 changes: 14 additions & 8 deletions src/formatter/Formatter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ package formatter;
import sys.FileSystem;
import sys.io.File;
#end
import haxe.CallStack;
import haxe.io.Path;
import tokentree.TokenTreeBuilder.TokenTreeEntryPoint;
import formatter.codedata.CodeLines;
import formatter.codedata.FormatterInputData;
import formatter.config.Config;
Expand All @@ -18,6 +15,9 @@ import formatter.marker.MarkSameLine;
import formatter.marker.MarkTokenText;
import formatter.marker.MarkWhitespace;
import formatter.marker.wrapping.MarkWrapping;
import haxe.CallStack;
import haxe.io.Path;
import tokentree.TokenTreeBuilder.TokenTreeEntryPoint;

enum Result {
Success(formattedCode:String);
Expand All @@ -28,8 +28,8 @@ enum Result {
class Formatter {
static inline var FORMATTER_JSON:String = "hxformat.json";

public static function format(input:FormatterInput, ?config:Config, ?lineSeparator:String, ?entryPoint:TokenTreeEntryPoint,
?range:FormatterInputRange):Result {
public static function format(input:FormatterInput, ?config:Config, ?lineSeparator:String, ?entryPoint:TokenTreeEntryPoint, ?range:FormatterInputRange,
?indentOffset:Int):Result {
if (config == null) {
config = new Config();
}
Expand All @@ -48,7 +48,8 @@ class Formatter {
config: config,
lineSeparator: lineSeparator,
entryPoint: entryPoint,
range: range
range: range,
indentOffset: indentOffset
};
return formatInputData(inputData);
#end
Expand All @@ -63,7 +64,8 @@ class Formatter {
config: config,
lineSeparator: lineSeparator,
entryPoint: entryPoint,
range: range
range: range,
indentOffset: indentOffset
};
return formatInputData(inputData);
case Tokens(tokenList, tokenTree, code, origin):
Expand All @@ -78,7 +80,8 @@ class Formatter {
config: config,
lineSeparator: lineSeparator,
entryPoint: entryPoint,
range: range
range: range,
indentOffset: indentOffset
};
return formatInputData(inputData);
}
Expand Down Expand Up @@ -119,6 +122,9 @@ class Formatter {

var indenter = new Indenter(config.indentation);
indenter.setParsedCode(parsedCode);
if (inputData.indentOffset != null) {
indenter.setIndentOffset(inputData.indentOffset);
}

var markTokenText = new MarkTokenText(config, parsedCode, indenter);
var markWhitespace = new MarkWhitespace(config, parsedCode, indenter);
Expand Down
3 changes: 2 additions & 1 deletion src/formatter/codedata/FormatterInputData.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package formatter.codedata;

import tokentree.TokenTreeBuilder.TokenTreeEntryPoint;
import formatter.config.Config;
import tokentree.TokenTreeBuilder.TokenTreeEntryPoint;

typedef FormatterInputData = {
var fileName:String;
Expand All @@ -12,6 +12,7 @@ typedef FormatterInputData = {
@:optional var entryPoint:TokenTreeEntryPoint;
@:optional var lineSeparator:String;
@:optional var range:FormatterInputRange;
@:optional var indentOffset:Int;
}

typedef FormatterInputRange = {
Expand Down
13 changes: 13 additions & 0 deletions src/formatter/marker/Indenter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import sys.io.FileOutput;
class Indenter {
var config:IndentationConfig;
var parsedCode:Null<ParsedCode>;
var indentOffset:Int;

public function new(config:IndentationConfig) {
this.config = config;
indentOffset = 0;
if (config.character.toLowerCase() == "tab") {
config.character = "\t";
}
Expand All @@ -21,6 +23,13 @@ class Indenter {
this.parsedCode = parsedCode;
}

public function setIndentOffset(indentOffset:Int) {
if (indentOffset < 0) {
indentOffset = 0;
}
this.indentOffset = indentOffset;
}

public function makeIndent(token:TokenTree):String {
return makeIndentString(calcIndent(token));
}
Expand All @@ -37,6 +46,10 @@ class Indenter {
}

public function calcIndent(token:TokenTree):Int {
return calcRealIndent(token) + indentOffset;
}

function calcRealIndent(token:TokenTree):Int {
if (token == null) {
return 0;
}
Expand Down

0 comments on commit cad782a

Please sign in to comment.