-
Notifications
You must be signed in to change notification settings - Fork 843
Hopefully settle line endings / position mapping #2879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5124913
e095fbb
110e328
a29d7c0
b9ee006
b15a5e3
6281436
8d80876
8c90fe7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ import ( | |
| ) | ||
|
|
||
| func GetIndentationForNode(n *ast.Node, ignoreActualIndentationRange *core.TextRange, sourceFile *ast.SourceFile, options *lsutil.FormatCodeSettings) int { | ||
| startline, startpos := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false)) | ||
| startline, startpos := scanner.GetECMALineAndByteOffsetOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it just me, or is it surprising that the formatter uses ECMAScript-based lines instead of LSP-based lines?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I totally agree, and we could plumb it differently, but I'm not sure it actually matters for these. |
||
| return getIndentationForNodeWorker(n, startline, startpos, ignoreActualIndentationRange /*indentationDelta*/, 0, sourceFile /*isNextChild*/, false, options) | ||
| } | ||
|
|
||
|
|
@@ -104,7 +104,7 @@ func getIndentationForNodeWorker( | |
| parent = current.Parent | ||
|
|
||
| if useTrueStart { | ||
| currentStartLine, currentStartCharacter = scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(current, sourceFile, false)) | ||
| currentStartLine, currentStartCharacter = scanner.GetECMALineAndByteOffsetOfPosition(sourceFile, scanner.GetTokenPosOfNode(current, sourceFile, false)) | ||
| } else { | ||
| currentStartLine = containingListOrParentStartLine | ||
| currentStartCharacter = containingListOrParentStartCharacter | ||
|
|
@@ -170,7 +170,7 @@ func getActualIndentationForListStartLine(list *ast.NodeList, sourceFile *ast.So | |
| if list == nil { | ||
| return -1 | ||
| } | ||
| line, char := scanner.GetECMALineAndCharacterOfPosition(sourceFile, list.Loc.Pos()) | ||
| line, char := scanner.GetECMALineAndByteOffsetOfPosition(sourceFile, list.Loc.Pos()) | ||
| return findColumnForFirstNonWhitespaceCharacterInLine(line, char, sourceFile, options) | ||
| } | ||
|
|
||
|
|
@@ -200,7 +200,7 @@ func deriveActualIndentationFromList(list *ast.NodeList, index int, sourceFile * | |
| } | ||
|
|
||
| func findColumnForFirstNonWhitespaceCharacterInLine(line int, char int, sourceFile *ast.SourceFile, options *lsutil.FormatCodeSettings) int { | ||
| lineStart := scanner.GetECMAPositionOfLineAndCharacter(sourceFile, line, 0) | ||
| lineStart := scanner.GetECMAPositionOfLineAndByteOffset(sourceFile, line, 0) | ||
| return FindFirstNonWhitespaceColumn(lineStart, lineStart+char, sourceFile, options) | ||
| } | ||
|
|
||
|
|
@@ -217,14 +217,11 @@ func FindFirstNonWhitespaceColumn(startPos int, endPos int, sourceFile *ast.Sour | |
| * value of 'column' for '$' is 6 (assuming that tab size is 4) | ||
| */ | ||
| func findFirstNonWhitespaceCharacterAndColumn(startPos int, endPos int, sourceFile *ast.SourceFile, options *lsutil.FormatCodeSettings) (character int, column int) { | ||
| character = 0 | ||
| column = 0 | ||
| text := sourceFile.Text() | ||
| for pos := startPos; pos < endPos; pos++ { | ||
| pos := startPos | ||
| for pos < endPos { | ||
| ch, size := utf8.DecodeRuneInString(text[pos:]) | ||
| if size == 0 && ch == utf8.RuneError { | ||
| continue // multibyte character - TODO: recognize non-tab multicolumn characters? ideographic space? | ||
| } | ||
| if !stringutil.IsWhiteSpaceSingleLine(ch) { | ||
| break | ||
| } | ||
|
|
@@ -235,9 +232,9 @@ func findFirstNonWhitespaceCharacterAndColumn(startPos int, endPos int, sourceFi | |
| column++ | ||
| } | ||
|
|
||
| character++ | ||
| pos += size | ||
| } | ||
| return character, column | ||
| return pos - startPos, column | ||
| } | ||
|
|
||
| func childStartsOnTheSameLineWithElseInIfStatement(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool { | ||
|
|
@@ -251,7 +248,7 @@ func childStartsOnTheSameLineWithElseInIfStatement(parent *ast.Node, child *ast. | |
| } | ||
|
|
||
| func getStartLineAndCharacterForNode(n *ast.Node, sourceFile *ast.SourceFile) (line int, character int) { | ||
| return scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false)) | ||
| return scanner.GetECMALineAndByteOffsetOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false)) | ||
| } | ||
|
|
||
| func getStartLineForNode(n *ast.Node, sourceFile *ast.SourceFile) int { | ||
|
|
@@ -361,7 +358,7 @@ func getContainingListOrParentStart(parent *ast.Node, child *ast.Node, sourceFil | |
| } else { | ||
| startPos = scanner.GetTokenPosOfNode(parent, sourceFile, false) | ||
| } | ||
| return scanner.GetECMALineAndCharacterOfPosition(sourceFile, startPos) | ||
| return scanner.GetECMALineAndByteOffsetOfPosition(sourceFile, startPos) | ||
| } | ||
|
|
||
| func isControlFlowEndingStatement(kind ast.Kind, parentKind ast.Kind) bool { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,11 +31,15 @@ func (w *textWriter) DecreaseIndent() { | |
| w.indent-- | ||
| } | ||
|
|
||
| func (w *textWriter) GetColumn() int { | ||
| // GetColumn returns the column position measured in UTF-16 code units | ||
| // for source map compatibility. | ||
| func (w *textWriter) GetColumn() core.UTF16Offset { | ||
| if w.lineStart { | ||
| return w.indent * w.indentSize | ||
| return core.UTF16Offset(w.indent * w.indentSize) | ||
| } | ||
jakebailey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return w.builder.Len() - w.linePos | ||
| // Count UTF-16 code units from the last line start. | ||
| // For ASCII-only output (the common case), this equals the byte count. | ||
| return core.UTF16Len(w.builder.String()[w.linePos:]) | ||
|
Comment on lines
+34
to
+42
|
||
| } | ||
|
|
||
| func (w *textWriter) GetIndent() int { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very odd, and arguably should actually be rune counts, since these are visible characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you just holding off on this to avoid a massive baseline avalanche?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No matter what we do, things will be just a little weird, so I just didn't change it