Skip to content

Commit 4ecfb00

Browse files
author
Omar Tawfik
committed
Added completion service + ShouldTriggerCompletion
1 parent 97303d4 commit 4ecfb00

File tree

5 files changed

+167
-1
lines changed

5 files changed

+167
-1
lines changed

vsintegration/src/FSharp.Editor/ColorizationService.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ type internal FSharpColorizationService() =
140140
| Some(options) ->
141141
let defines = CompilerEnvironment.GetCompilationDefinesForEditing(document.Name, options.OtherOptions |> Seq.toList)
142142
if sourceTextTask.Status = TaskStatus.RanToCompletion then
143-
result.AddRange(FSharpColorizationService.GetColorizationData(sourceTextTask.Result, textSpan, None, defines, cancellationToken))
143+
result.AddRange(FSharpColorizationService.GetColorizationData(sourceTextTask.Result, textSpan, Some(document.FilePath), defines, cancellationToken))
144144
| None -> ()
145145
, cancellationToken)
146146

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.Collections.Immutable
10+
open System.Threading
11+
open System.Threading.Tasks
12+
open System.Linq
13+
14+
open Microsoft.CodeAnalysis
15+
open Microsoft.CodeAnalysis.Completion
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
21+
open Microsoft.CodeAnalysis.Host.Mef
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.SourceCodeServices
30+
open Microsoft.FSharp.Compiler.Range
31+
32+
type internal FSharpCompletionService(workspace: Workspace) =
33+
inherit CompletionServiceWithProviders(workspace)
34+
35+
let builtInProviders = ImmutableArray.Create<CompletionProvider>(FSharpCompletionProvider(workspace))
36+
let completionRules = CompletionRules.Default.WithDismissIfEmpty(true).WithDismissIfLastCharacterDeleted(true).WithDefaultEnterKeyRule(EnterKeyRule.Never)
37+
38+
override this.Language = FSharpCommonConstants.FSharpLanguageName
39+
override this.GetBuiltInProviders() = builtInProviders
40+
override this.GetRules() = completionRules
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.Collections.Immutable
10+
open System.Threading
11+
open System.Threading.Tasks
12+
open System.Linq
13+
14+
open Microsoft.CodeAnalysis
15+
open Microsoft.CodeAnalysis.Completion
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
21+
open Microsoft.CodeAnalysis.Host.Mef
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.SourceCodeServices
30+
open Microsoft.FSharp.Compiler.Range
31+
32+
[<Shared>]
33+
[<ExportLanguageServiceFactory(typeof<CompletionService>, FSharpCommonConstants.FSharpLanguageName)>]
34+
type internal FSharpCompletionServiceFactory() =
35+
interface ILanguageServiceFactory with
36+
member this.CreateLanguageService(hostLanguageServices: HostLanguageServices) : ILanguageService =
37+
upcast new FSharpCompletionService(hostLanguageServices.WorkspaceServices.Workspace)

vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@
5555
<Compile Include="DocumentDiagnosticAnalyzer.fs">
5656
<Link>Diagnostics\DocumentDiagnosticAnalyzer.fs</Link>
5757
</Compile>
58+
<Compile Include="CompletionProvider.fs">
59+
<Link>Completion\CompletionProvider.fs</Link>
60+
</Compile>
61+
<Compile Include="CompletionService.fs">
62+
<Link>Completion\CompletionService.fs</Link>
63+
</Compile>
64+
<Compile Include="CompletionServiceFactory.fs">
65+
<Link>Completion\CompletionServiceFactory.fs</Link>
66+
</Compile>
5867
<Compile Include="ContentType.fs" />
5968
</ItemGroup>
6069
<ItemGroup>

0 commit comments

Comments
 (0)