Skip to content

Commit

Permalink
fixed indentation of refactored snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe committed Nov 30, 2024
1 parent ea2ed9b commit 10018f8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
5 changes: 2 additions & 3 deletions haxe_libraries/formatter.hxml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# @install: lix --silent download "haxelib:/formatter#1.17.1" into formatter/1.17.1/haxelib
# @run: haxelib run-dir formatter "${HAXE_LIBCACHE}/formatter/1.17.1/haxelib"
-cp ${HAXE_LIBCACHE}/formatter/1.17.1/haxelib/src
# @install: lix --silent download "gh://github.com/HaxeCheckstyle/haxe-formatter#cad782a6571cea82c324fc97a83fb138b740b1bd" into formatter/1.17.1/github/cad782a6571cea82c324fc97a83fb138b740b1bd
-cp ${HAXE_LIBCACHE}/formatter/1.17.1/github/cad782a6571cea82c324fc97a83fb138b740b1bd/src
-D formatter=1.17.1
4 changes: 2 additions & 2 deletions haxe_libraries/rename.hxml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @install: lix --silent download "gh://github.com/HaxeCheckstyle/haxe-rename#6fb14295b160cba3cd98f31a10c6688356781ac8" into rename/2.3.1/github/6fb14295b160cba3cd98f31a10c6688356781ac8
-cp ${HAXE_LIBCACHE}/rename/2.3.1/github/6fb14295b160cba3cd98f31a10c6688356781ac8/src
# @install: lix --silent download "gh://github.com/HaxeCheckstyle/haxe-rename#e627131c70aeca2be39ea62fc407ca613470d682" into rename/2.3.1/github/e627131c70aeca2be39ea62fc407ca613470d682
-cp ${HAXE_LIBCACHE}/rename/2.3.1/github/e627131c70aeca2be39ea62fc407ca613470d682/src
-D rename=2.3.1
46 changes: 37 additions & 9 deletions src/haxeLanguageServer/features/haxe/refactoring/EditDoc.hx
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,49 @@ class EditDoc implements IEditableDocument {
ignoreIfNotExists: false
}
});
case ReplaceText(text, pos, format):
if (format) {
text = FormatterHelper.formatSnippet(filePath, text, TokenTreeEntryPoint.FieldLevel);
}
edits.push({range: posToRange(pos), newText: text});
case InsertText(text, pos, format):
if (format) {
text = FormatterHelper.formatSnippet(filePath, text, TokenTreeEntryPoint.FieldLevel);
}
case ReplaceText(text, pos, f):
final range = posToRange(pos);
text = correctFirstLineIndent(f, text, range);
edits.push({range: range, newText: text});
case InsertText(text, pos, f):
final range = posToRange(pos);
text = correctFirstLineIndent(f, text, range);
edits.push({range: posToRange(pos), newText: text});
case RemoveText(pos):
edits.push({range: posToRange(pos), newText: ""});
}
}

function correctFirstLineIndent(f:refactor.edits.FormatType, text:String, range:Range):String {
switch (f) {
case NoFormat:
case Format(indentOffset):
text = FormatterHelper.formatSnippet(filePath, text, TokenTreeEntryPoint.FieldLevel, indentOffset);
if (range.start.character != 0) {
var doc:Null<HaxeDocument> = context.documents.getHaxe(filePath.toUri());
if (doc != null) {
final beforeRange:Range = {
start: {
line: range.start.line,
character: 0
},
end: {
line: range.start.line,
character: range.start.character
}
};
var beforeText = doc.getText(beforeRange);
if (beforeText.trim().length == 0) {
range.start.character = 0;
} else {
text = text.ltrim();
}
}
}
}
return text;
}

public function posToRange(pos:refactor.discover.IdentifierPos):Range {
if (!FileSystem.exists(filePath.toString())) {
var posNull:Position = {line: 0, character: 0};
Expand Down
4 changes: 2 additions & 2 deletions src/haxeLanguageServer/helper/FormatterHelper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class FormatterHelper {
return code;
}

public static function formatSnippet(path:FsPath, code:String, entryPoint:TokenTreeEntryPoint):String {
public static function formatSnippet(path:FsPath, code:String, entryPoint:TokenTreeEntryPoint, ?indentOffset:Int):String {
final config = Formatter.loadConfig(path.toString());
switch Formatter.format(Code(code, Snippet), config, null, entryPoint) {
switch Formatter.format(Code(code, Snippet), config, null, entryPoint, indentOffset) {
case Success(formattedCode):
return formattedCode;
case Failure(_):
Expand Down

0 comments on commit 10018f8

Please sign in to comment.