Skip to content
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

Fix completion with backticks, underscores, numbers #10500

Merged
merged 3 commits into from Nov 25, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions vsintegration/src/FSharp.Editor/Completion/CompletionService.fs
Expand Up @@ -10,9 +10,12 @@ open Microsoft.CodeAnalysis.Completion
open Microsoft.CodeAnalysis.Host
open Microsoft.CodeAnalysis.Host.Mef
open Microsoft.CodeAnalysis.ExternalAccess.FSharp.Completion
open Microsoft.CodeAnalysis.Text

open Microsoft.VisualStudio.Shell

open FSharp.Compiler.PrettyNaming

type internal FSharpCompletionService
(
workspace: Workspace,
Expand Down Expand Up @@ -43,6 +46,41 @@ type internal FSharpCompletionService
.WithDismissIfLastCharacterDeleted(true)
.WithDefaultEnterKeyRule(enterKeyRule)

override _.GetDefaultCompletionListSpan( sourceText , caretIndex ) =

let mutable startIndex = 0
let mutable endIndex = 0

// Get single line text and index
let textLines = sourceText.Lines
let lineText = textLines.GetLineFromPosition(caretIndex).ToString()
let lineCaretIndex = textLines.GetLinePosition(caretIndex).Character

// Check for enclosing backticks or leading backticks, else capture valid identifier characters
// TODO Replace naive backtick check with safer alternative
match lineText.IndexOf "``", lineText.LastIndexOf "``" with
cartermp marked this conversation as resolved.
Show resolved Hide resolved
| startTickIndex, endTickIndex when startTickIndex > -1 && endTickIndex > -1 && startTickIndex <> endTickIndex && lineCaretIndex >= startTickIndex && lineCaretIndex <= endTickIndex + 2 ->
// Cursor is at or between a pair of double ticks, select enclosed range including ticks as identifier
startIndex <- startTickIndex
endIndex <- endTickIndex + 2
| startTickIndex, endTickIndex when startTickIndex > -1 && endTickIndex > -1 && startTickIndex = endTickIndex && lineCaretIndex >= startTickIndex ->
// Cursor is at or after double ticks with none following, select ticks and all following text as identifier
startIndex <- startTickIndex
endIndex <- lineText.Length
| _ ->
// No ticks, capture identifier-part chars backward and forward from cursor as identifier
startIndex <- lineCaretIndex
while startIndex > 0 && IsIdentifierPartCharacter lineText.[startIndex - 1] do startIndex <- startIndex - 1
endIndex <- lineCaretIndex
if startIndex <> lineCaretIndex then
while endIndex < lineText.Length && IsIdentifierPartCharacter lineText.[endIndex] do endIndex <- endIndex + 1

// Translate line index back to document index
startIndex <- caretIndex - (lineCaretIndex - startIndex)
endIndex <- caretIndex + (endIndex - lineCaretIndex)

TextSpan.FromBounds(startIndex, endIndex)

[<Shared>]
[<ExportLanguageServiceFactory(typeof<CompletionService>, FSharpConstants.FSharpLanguageName)>]
type internal FSharpCompletionServiceFactory
Expand Down