Skip to content

Commit

Permalink
fix range comparisons for symbol based lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel committed May 29, 2022
1 parent e09ffd2 commit be7b765
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
24 changes: 16 additions & 8 deletions src/FsAutoComplete.Core/Commands.fs
Expand Up @@ -1172,23 +1172,31 @@ type Commands(checker: FSharpCompilerServiceChecker, state: State, hasAnalyzers:
// the checker gives us back wacky ranges sometimes, so what we're going to do is check if the text of the triggering
// symbol use is in each of the ranges we plan to rename, and if we're looking at a range that is _longer_ than our rename range,
// do some splicing to find just the range we need to replace.
let symbolRange = symbol.DefinitionRange
// TODO: figure out where the caps are coming from in the compilation, maybe something wrong in the
let symbolFile, symbolRange =
let symbolRange = symbol.DefinitionRange

let symbolFile =
if System.Char.IsUpper(symbolRange.FileName[0]) then
UMX.tag (
string (System.Char.ToLowerInvariant symbolRange.FileName[0])
+ (symbolRange.FileName.Substring(1))
)
// we've got a case where the compiler is reading things from the file system that we'd rather it not -
// if we're adjusting the range's filename, we need to construct a whole new range or else indexing won't work
//
let fileName =
UMX.tag (
string (System.Char.ToLowerInvariant symbolRange.FileName[0])
+ (symbolRange.FileName.Substring(1))
)

let newRange = Range.mkRange fileName symbolRange.Start symbolRange.End
UMX.tag fileName, newRange
else
UMX.tag symbolRange.FileName
UMX.tag symbolRange.FileName, symbolRange

let symbolFileText =
state.TryGetFileSource(symbolFile)
|> Result.fold id (fun e -> failwith "blah blah")

let symbolText =
symbolFileText[symbol.DefinitionRange]
symbolFileText[symbolRange]
|> Result.fold id (fun e -> failwith "Unable to get text for initial symbol use")

let projects =
Expand Down
11 changes: 7 additions & 4 deletions src/FsAutoComplete.Core/FileSystem.fs
Expand Up @@ -68,6 +68,9 @@ type NamedText(fileName: string<LocalPath>, str: string) =
(let (endLine, endChar) = lastCharPos.Value
Position.mkPos endLine endChar)

let totalRange =
lazy (Range.mkRange (UMX.untag fileName) Position.pos0 safeLastCharPos.Value)

member _.String = str

override _.GetHashCode() = str.GetHashCode()
Expand All @@ -92,8 +95,8 @@ type NamedText(fileName: string<LocalPath>, str: string) =

/// Cached representation of the entire contents of the file, for inclusion checks
member x.TotalRange =
Range.mkRange (UMX.untag fileName) Position.pos0 x.LastFilePosition
member x.TotalRange = totalRange.Value


/// Provides safe access to a substring of the file via FCS-provided Range
member x.GetText(m: FSharp.Compiler.Text.Range) : Result<string, string> =
Expand Down Expand Up @@ -213,11 +216,11 @@ type NamedText(fileName: string<LocalPath>, str: string) =
}

/// Safe access to the contents of a file by Range
member x.Item
member inline x.Item
with get (m: FSharp.Compiler.Text.Range) = x.GetText(m)

/// Safe access to the char in a file by Position
member x.Item
member inline x.Item
with get (pos: FSharp.Compiler.Text.Position) = x.TryGetChar(pos)

member private x.Walk
Expand Down
6 changes: 3 additions & 3 deletions src/FsAutoComplete.Core/Utils.fs
Expand Up @@ -767,7 +767,7 @@ module Indentation =


type FSharpSymbol with
member x.XDoc =
member inline x.XDoc =
match x with
| :? FSharpEntity as e -> e.XmlDoc
| :? FSharpUnionCase as u -> u.XmlDoc
Expand All @@ -779,7 +779,7 @@ type FSharpSymbol with
| :? FSharpParameter -> FSharpXmlDoc.None
| _ -> failwith $"cannot fetch xmldoc for unknown FSharpSymbol subtype {x.GetType().FullName}"

member x.XSig =
member inline x.XSig =
match x with
| :? FSharpEntity as e -> e.XmlDocSig
| :? FSharpUnionCase as u -> u.XmlDocSig
Expand All @@ -791,7 +791,7 @@ type FSharpSymbol with
| :? FSharpParameter -> ""
| _ -> failwith $"cannot fetch XmlDocSig for unknown FSharpSymbol subtype {x.GetType().FullName}"

member x.DefinitionRange =
member inline x.DefinitionRange =
match x with
| :? FSharpEntity as e -> e.DeclarationLocation
| :? FSharpUnionCase as u -> u.DeclarationLocation
Expand Down

0 comments on commit be7b765

Please sign in to comment.