|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +namespace Microsoft.VisualStudio.FSharp.Editor |
| 4 | + |
| 5 | +open System |
| 6 | +open System.Composition |
| 7 | +open System.Collections.Concurrent |
| 8 | +open System.Collections.Generic |
| 9 | +open System.Threading |
| 10 | +open System.Threading.Tasks |
| 11 | +open System.Linq |
| 12 | + |
| 13 | +open Microsoft.CodeAnalysis |
| 14 | +open Microsoft.CodeAnalysis.Completion |
| 15 | +open Microsoft.CodeAnalysis.Classification |
| 16 | +open Microsoft.CodeAnalysis.Editor |
| 17 | +open Microsoft.CodeAnalysis.Editor.Implementation.Debugging |
| 18 | +open Microsoft.CodeAnalysis.Editor.Shared.Utilities |
| 19 | +open Microsoft.CodeAnalysis.Formatting |
| 20 | +open Microsoft.CodeAnalysis.Host.Mef |
| 21 | +open Microsoft.CodeAnalysis.Options |
| 22 | +open Microsoft.CodeAnalysis.Text |
| 23 | + |
| 24 | +open Microsoft.VisualStudio.FSharp.LanguageService |
| 25 | +open Microsoft.VisualStudio.Text |
| 26 | +open Microsoft.VisualStudio.Text.Tagging |
| 27 | + |
| 28 | +open Microsoft.FSharp.Compiler.Parser |
| 29 | +open Microsoft.FSharp.Compiler.Range |
| 30 | +open Microsoft.FSharp.Compiler.SourceCodeServices |
| 31 | + |
| 32 | +type internal FSharpCompletionProvider(workspace: Workspace) = |
| 33 | + inherit CompletionProvider() |
| 34 | + |
| 35 | + let completionTriggers = [ '.' ] |
| 36 | + |
| 37 | + override this.ShouldTriggerCompletion(sourceText: SourceText, caretPosition: int, trigger: CompletionTrigger, _: OptionSet) = |
| 38 | + // Skip if we are at the start of a document |
| 39 | + if caretPosition = 0 then |
| 40 | + false |
| 41 | + |
| 42 | + // Skip if it was triggered by an operation other than insertion |
| 43 | + else if not (trigger.Kind = CompletionTriggerKind.Insertion) then |
| 44 | + false |
| 45 | + |
| 46 | + // Skip if we are not on a completion trigger |
| 47 | + else if not (completionTriggers |> Seq.contains(sourceText.[caretPosition - 1])) then |
| 48 | + false |
| 49 | + |
| 50 | + // Trigger completion if we are on a valid classification type |
| 51 | + else |
| 52 | + let documentId = workspace.GetDocumentIdInCurrentContext(sourceText.Container) |
| 53 | + let document = workspace.CurrentSolution.GetDocument(documentId) |
| 54 | + |
| 55 | + match FSharpLanguageService.GetOptions(document.Project.Id) with |
| 56 | + | Some(options) -> |
| 57 | + |
| 58 | + let triggerPosition = caretPosition - 1 |
| 59 | + let textLine = sourceText.Lines.GetLineFromPosition(triggerPosition) |
| 60 | + let defines = CompilerEnvironment.GetCompilationDefinesForEditing(document.Name, options.OtherOptions |> Seq.toList) |
| 61 | + let classifiedSpanOption = |
| 62 | + FSharpColorizationService.GetColorizationData(sourceText, textLine.Span, Some(document.FilePath), defines, CancellationToken.None) |
| 63 | + |> Seq.tryFind(fun classifiedSpan -> classifiedSpan.TextSpan.Contains(triggerPosition)) |
| 64 | + |
| 65 | + match classifiedSpanOption with |
| 66 | + | None -> false |
| 67 | + | Some(classifiedSpan) -> |
| 68 | + match classifiedSpan.ClassificationType with |
| 69 | + | ClassificationTypeNames.Comment -> false |
| 70 | + | ClassificationTypeNames.StringLiteral -> false |
| 71 | + | ClassificationTypeNames.ExcludedCode -> false |
| 72 | + | _ -> true // anything else is a valid classification type |
| 73 | + |
| 74 | + | None -> false |
| 75 | + |
| 76 | + override this.ProvideCompletionsAsync(context: Microsoft.CodeAnalysis.Completion.CompletionContext): Task = |
| 77 | + Task.CompletedTask |
| 78 | + |
| 79 | + override this.GetDescriptionAsync(document: Document, item: CompletionItem, cancellationToken: CancellationToken) = |
| 80 | + Task.FromResult(CompletionDescription.Empty) |
0 commit comments