diff --git a/CHANGELOG.md b/CHANGELOG.md index c52ec1e..fa79e10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/formatter/Formatter.hx b/src/formatter/Formatter.hx index f6e5e09..a2061e1 100644 --- a/src/formatter/Formatter.hx +++ b/src/formatter/Formatter.hx @@ -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; @@ -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); @@ -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(); } @@ -48,7 +48,8 @@ class Formatter { config: config, lineSeparator: lineSeparator, entryPoint: entryPoint, - range: range + range: range, + indentOffset: indentOffset }; return formatInputData(inputData); #end @@ -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): @@ -78,7 +80,8 @@ class Formatter { config: config, lineSeparator: lineSeparator, entryPoint: entryPoint, - range: range + range: range, + indentOffset: indentOffset }; return formatInputData(inputData); } @@ -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); diff --git a/src/formatter/codedata/FormatterInputData.hx b/src/formatter/codedata/FormatterInputData.hx index d55e672..f80b64e 100644 --- a/src/formatter/codedata/FormatterInputData.hx +++ b/src/formatter/codedata/FormatterInputData.hx @@ -1,7 +1,7 @@ package formatter.codedata; -import tokentree.TokenTreeBuilder.TokenTreeEntryPoint; import formatter.config.Config; +import tokentree.TokenTreeBuilder.TokenTreeEntryPoint; typedef FormatterInputData = { var fileName:String; @@ -12,6 +12,7 @@ typedef FormatterInputData = { @:optional var entryPoint:TokenTreeEntryPoint; @:optional var lineSeparator:String; @:optional var range:FormatterInputRange; + @:optional var indentOffset:Int; } typedef FormatterInputRange = { diff --git a/src/formatter/marker/Indenter.hx b/src/formatter/marker/Indenter.hx index cd813f7..b256ca9 100644 --- a/src/formatter/marker/Indenter.hx +++ b/src/formatter/marker/Indenter.hx @@ -9,9 +9,11 @@ import sys.io.FileOutput; class Indenter { var config:IndentationConfig; var parsedCode:Null; + var indentOffset:Int; public function new(config:IndentationConfig) { this.config = config; + indentOffset = 0; if (config.character.toLowerCase() == "tab") { config.character = "\t"; } @@ -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)); } @@ -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; }