Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: Fix crash if there is no newline at the end of the last line #83

Merged
merged 1 commit into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/CSharpLanguageServer/RoslynHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,18 @@ let roslynTagToLspCompletion tag =
let lspPositionForRoslynLinePosition (pos: LinePosition): Types.Position =
{ Line = pos.Line ; Character = pos.Character }

let roslynLinePositionForLspPosition (pos: Types.Position) =
LinePosition(pos.Line, pos.Character)
let roslynLinePositionForLspPosition (lines: TextLineCollection) (pos: Types.Position) =
if pos.Line < 0 then
LinePosition(0, 0)
else if pos.Line >= lines.Count then
LinePosition(lines.Count - 1, lines[lines.Count - 1].EndIncludingLineBreak - lines[lines.Count - 1].Start)
else
LinePosition(pos.Line, pos.Character)

let roslynLinePositionSpanForLspRange (range: Types.Range) =
let roslynLinePositionSpanForLspRange (lines: TextLineCollection) (range: Types.Range) =
LinePositionSpan(
roslynLinePositionForLspPosition range.Start,
roslynLinePositionForLspPosition range.End)
roslynLinePositionForLspPosition lines range.Start,
roslynLinePositionForLspPosition lines range.End)

let lspRangeForRoslynLinePosSpan (pos: LinePositionSpan): Types.Range =
{ Start = lspPositionForRoslynLinePosition pos.Start
Expand All @@ -82,7 +87,7 @@ let applyLspContentChangesOnRoslynSourceText
match change.Range with
| Some changeRange ->
let changeTextSpan =
changeRange |> roslynLinePositionSpanForLspRange
changeRange |> roslynLinePositionSpanForLspRange sourceText.Lines
|> sourceText.Lines.GetTextSpan

TextChange(changeTextSpan, change.Text) |> sourceText.WithChanges
Expand Down
12 changes: 6 additions & 6 deletions src/CSharpLanguageServer/Server.fs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ let setupServerHandlers settings (lspClient: LspClient) =

let textSpan =
actionParams.Range
|> roslynLinePositionSpanForLspRange
|> roslynLinePositionSpanForLspRange docText.Lines
|> docText.Lines.GetTextSpan

let! roslynCodeActions =
Expand Down Expand Up @@ -548,7 +548,7 @@ let setupServerHandlers settings (lspClient: LspClient) =

let textSpan =
resolutionData.Value.Range
|> roslynLinePositionSpanForLspRange
|> roslynLinePositionSpanForLspRange docText.Lines
|> docText.Lines.GetTextSpan

let! roslynCodeActions =
Expand Down Expand Up @@ -871,8 +871,8 @@ let setupServerHandlers settings (lspClient: LspClient) =
|> Option.defaultValue false

let linePositionSpan = LinePositionSpan(
roslynLinePositionForLspPosition prepareRename.Position,
roslynLinePositionForLspPosition prepareRename.Position)
roslynLinePositionForLspPosition docText.Lines prepareRename.Position,
roslynLinePositionForLspPosition docText.Lines prepareRename.Position)

let textSpan = docText.Lines.GetTextSpan(linePositionSpan)

Expand Down Expand Up @@ -1120,7 +1120,7 @@ let setupServerHandlers settings (lspClient: LspClient) =
match range with
| Some r ->
r
|> roslynLinePositionSpanForLspRange
|> roslynLinePositionSpanForLspRange sourceText.Lines
|> sourceText.Lines.GetTextSpan
| None ->
TextSpan(0, sourceText.Length)
Expand Down Expand Up @@ -1269,7 +1269,7 @@ let setupServerHandlers settings (lspClient: LspClient) =
let! sourceText = doc.GetTextAsync() |> Async.AwaitTask
let textSpan =
inlayHintParams.Range
|> roslynLinePositionSpanForLspRange
|> roslynLinePositionSpanForLspRange sourceText.Lines
|> sourceText.Lines.GetTextSpan

let inlayHints =
Expand Down