-
Notifications
You must be signed in to change notification settings - Fork 847
Added Roslyn Completion Service #1534
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // 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. | ||
|
|
||
| namespace Microsoft.VisualStudio.FSharp.Editor | ||
|
|
||
| open System | ||
| open System.Composition | ||
| open System.Collections.Concurrent | ||
| open System.Collections.Generic | ||
| open System.Collections.Immutable | ||
| open System.Threading | ||
| open System.Threading.Tasks | ||
| open System.Linq | ||
| open System.Runtime.CompilerServices | ||
|
|
||
| open Microsoft.CodeAnalysis | ||
| open Microsoft.CodeAnalysis.Completion | ||
| open Microsoft.CodeAnalysis.Classification | ||
| open Microsoft.CodeAnalysis.Editor | ||
| open Microsoft.CodeAnalysis.Editor.Implementation.Debugging | ||
| open Microsoft.CodeAnalysis.Editor.Shared.Utilities | ||
| open Microsoft.CodeAnalysis.Formatting | ||
| open Microsoft.CodeAnalysis.Host.Mef | ||
| open Microsoft.CodeAnalysis.Options | ||
| open Microsoft.CodeAnalysis.Text | ||
|
|
||
| open Microsoft.VisualStudio.FSharp.LanguageService | ||
| open Microsoft.VisualStudio.Text | ||
| open Microsoft.VisualStudio.Text.Tagging | ||
| open Microsoft.VisualStudio.Shell | ||
| open Microsoft.VisualStudio.Shell.Interop | ||
|
|
||
| open Microsoft.FSharp.Compiler.Parser | ||
| open Microsoft.FSharp.Compiler.Range | ||
| open Microsoft.FSharp.Compiler.SourceCodeServices | ||
|
|
||
| type internal FSharpCompletionProvider(workspace: Workspace, serviceProvider: SVsServiceProvider) = | ||
| inherit CompletionProvider() | ||
|
|
||
| static let completionTriggers = [ '.' ] | ||
| static let declarationItemsCache = ConditionalWeakTable<string, FSharpDeclarationListItem>() | ||
|
|
||
| let xmlMemberIndexService = serviceProvider.GetService(typeof<IVsXMLMemberIndexService>) :?> IVsXMLMemberIndexService | ||
| let documentationBuilder = XmlDocumentation.CreateDocumentationBuilder(xmlMemberIndexService, serviceProvider.DTE) | ||
|
|
||
| static member ShouldTriggerCompletionAux(sourceText: SourceText, caretPosition: int, trigger: CompletionTriggerKind, filePath: string, defines: string list) = | ||
| // Skip if we are at the start of a document | ||
| if caretPosition = 0 then | ||
| false | ||
|
|
||
| // Skip if it was triggered by an operation other than insertion | ||
| else if not (trigger = CompletionTriggerKind.Insertion) then | ||
| false | ||
|
|
||
| // Skip if we are not on a completion trigger | ||
| else if not (completionTriggers |> Seq.contains(sourceText.[caretPosition - 1])) then | ||
| false | ||
|
|
||
| // Trigger completion if we are on a valid classification type | ||
| else | ||
| let triggerPosition = caretPosition - 1 | ||
| let textLine = sourceText.Lines.GetLineFromPosition(triggerPosition) | ||
| let classifiedSpanOption = | ||
| FSharpColorizationService.GetColorizationData(sourceText, textLine.Span, Some(filePath), defines, CancellationToken.None) | ||
| |> Seq.tryFind(fun classifiedSpan -> classifiedSpan.TextSpan.Contains(triggerPosition)) | ||
|
|
||
| match classifiedSpanOption with | ||
| | None -> false | ||
| | Some(classifiedSpan) -> | ||
| match classifiedSpan.ClassificationType with | ||
| | ClassificationTypeNames.Comment -> false | ||
| | ClassificationTypeNames.StringLiteral -> false | ||
| | ClassificationTypeNames.ExcludedCode -> false | ||
| | _ -> true // anything else is a valid classification type | ||
|
|
||
| static member ProvideCompletionsAsyncAux(sourceText: SourceText, caretPosition: int, options: FSharpProjectOptions, filePath: string, textVersionHash: int) = async { | ||
| let! parseResults = FSharpChecker.Instance.ParseFileInProject(filePath, sourceText.ToString(), options) | ||
| let! checkFileAnswer = FSharpChecker.Instance.CheckFileInProject(parseResults, filePath, textVersionHash, sourceText.ToString(), options) | ||
| let checkFileResults = match checkFileAnswer with | ||
| | FSharpCheckFileAnswer.Aborted -> failwith "Compilation isn't complete yet" | ||
| | FSharpCheckFileAnswer.Succeeded(results) -> results | ||
|
|
||
| let textLine = sourceText.Lines.GetLineFromPosition(caretPosition) | ||
| let textLineNumber = textLine.LineNumber + 1 // Roslyn line numbers are zero-based | ||
| let qualifyingNames, partialName = QuickParse.GetPartialLongNameEx(textLine.ToString(), caretPosition - textLine.Start - 1) | ||
| let! declarations = checkFileResults.GetDeclarationListInfo(Some(parseResults), textLineNumber, caretPosition, textLine.ToString(), qualifyingNames, partialName) | ||
|
|
||
| let results = List<CompletionItem>() | ||
|
|
||
| for declarationItem in declarations.Items do | ||
| let completionItem = CompletionItem.Create(declarationItem.Name) | ||
| declarationItemsCache.Add(completionItem.DisplayText, declarationItem) | ||
| results.Add(completionItem) | ||
|
|
||
| return results | ||
| } | ||
|
|
||
|
|
||
| override this.ShouldTriggerCompletion(sourceText: SourceText, caretPosition: int, trigger: CompletionTrigger, _: OptionSet) = | ||
| let documentId = workspace.GetDocumentIdInCurrentContext(sourceText.Container) | ||
| let document = workspace.CurrentSolution.GetDocument(documentId) | ||
|
|
||
| match FSharpLanguageService.GetOptions(document.Project.Id) with | ||
| | None -> false | ||
| | Some(options) -> | ||
| let defines = CompilerEnvironment.GetCompilationDefinesForEditing(document.Name, options.OtherOptions |> Seq.toList) | ||
| FSharpCompletionProvider.ShouldTriggerCompletionAux(sourceText, caretPosition, trigger.Kind, document.FilePath, defines) | ||
|
|
||
| override this.ProvideCompletionsAsync(context: Microsoft.CodeAnalysis.Completion.CompletionContext) = | ||
| let computation = async { | ||
| match FSharpLanguageService.GetOptions(context.Document.Project.Id) with | ||
| | Some(options) -> | ||
| let! sourceText = context.Document.GetTextAsync(context.CancellationToken) |> Async.AwaitTask | ||
| let! textVersion = context.Document.GetTextVersionAsync(context.CancellationToken) |> Async.AwaitTask | ||
| let! results = FSharpCompletionProvider.ProvideCompletionsAsyncAux(sourceText, context.Position, options, context.Document.FilePath, textVersion.GetHashCode()) | ||
| context.AddItems(results) | ||
| | None -> () | ||
| } | ||
|
|
||
| Task.Run(CommonRoslynHelpers.GetTaskAction(computation), context.CancellationToken) | ||
|
|
||
| override this.GetDescriptionAsync(_: Document, completionItem: CompletionItem, cancellationToken: CancellationToken): Task<CompletionDescription> = | ||
| let computation = async { | ||
| let exists, declarationItem = declarationItemsCache.TryGetValue(completionItem.DisplayText) | ||
| if exists then | ||
| let! description = declarationItem.DescriptionTextAsync | ||
| let datatipText = XmlDocumentation.BuildDataTipText(documentationBuilder, description) | ||
| return CompletionDescription.FromText(datatipText) | ||
| else | ||
| return CompletionDescription.Empty | ||
| } | ||
|
|
||
| Async.StartAsTask(computation, TaskCreationOptions.None, cancellationToken) | ||
| .ContinueWith(CommonRoslynHelpers.GetCompletedTaskResult, cancellationToken) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // 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. | ||
|
|
||
| namespace Microsoft.VisualStudio.FSharp.Editor | ||
|
|
||
| open System | ||
| open System.Composition | ||
| open System.Collections.Concurrent | ||
| open System.Collections.Generic | ||
| open System.Collections.Immutable | ||
| open System.Threading | ||
| open System.Threading.Tasks | ||
| open System.Linq | ||
|
|
||
| open Microsoft.CodeAnalysis | ||
| open Microsoft.CodeAnalysis.Completion | ||
| open Microsoft.CodeAnalysis.Editor | ||
| open Microsoft.CodeAnalysis.Editor.Implementation.Debugging | ||
| open Microsoft.CodeAnalysis.Editor.Shared.Utilities | ||
| open Microsoft.CodeAnalysis.Formatting | ||
| open Microsoft.CodeAnalysis.Host | ||
| open Microsoft.CodeAnalysis.Host.Mef | ||
| open Microsoft.CodeAnalysis.Text | ||
|
|
||
| open Microsoft.VisualStudio.FSharp.LanguageService | ||
| open Microsoft.VisualStudio.Text | ||
| open Microsoft.VisualStudio.Text.Tagging | ||
| open Microsoft.VisualStudio.Shell | ||
|
|
||
| open Microsoft.FSharp.Compiler.Parser | ||
| open Microsoft.FSharp.Compiler.SourceCodeServices | ||
| open Microsoft.FSharp.Compiler.Range | ||
|
|
||
| type internal FSharpCompletionService(workspace: Workspace, serviceProvider: SVsServiceProvider) = | ||
| inherit CompletionServiceWithProviders(workspace) | ||
|
|
||
| let builtInProviders = ImmutableArray.Create<CompletionProvider>(FSharpCompletionProvider(workspace, serviceProvider)) | ||
| let completionRules = CompletionRules.Default.WithDismissIfEmpty(true).WithDismissIfLastCharacterDeleted(true).WithDefaultEnterKeyRule(EnterKeyRule.Never) | ||
|
|
||
| override this.Language = FSharpCommonConstants.FSharpLanguageName | ||
| override this.GetBuiltInProviders() = builtInProviders | ||
| override this.GetRules() = completionRules |
38 changes: 38 additions & 0 deletions
38
vsintegration/src/FSharp.Editor/CompletionServiceFactory.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // 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. | ||
|
|
||
| namespace Microsoft.VisualStudio.FSharp.Editor | ||
|
|
||
| open System | ||
| open System.Composition | ||
| open System.Collections.Concurrent | ||
| open System.Collections.Generic | ||
| open System.Collections.Immutable | ||
| open System.Threading | ||
| open System.Threading.Tasks | ||
| open System.Linq | ||
|
|
||
| open Microsoft.CodeAnalysis | ||
| open Microsoft.CodeAnalysis.Completion | ||
| open Microsoft.CodeAnalysis.Editor | ||
| open Microsoft.CodeAnalysis.Editor.Implementation.Debugging | ||
| open Microsoft.CodeAnalysis.Editor.Shared.Utilities | ||
| open Microsoft.CodeAnalysis.Formatting | ||
| open Microsoft.CodeAnalysis.Host | ||
| open Microsoft.CodeAnalysis.Host.Mef | ||
| open Microsoft.CodeAnalysis.Text | ||
|
|
||
| open Microsoft.VisualStudio.FSharp.LanguageService | ||
| open Microsoft.VisualStudio.Text | ||
| open Microsoft.VisualStudio.Text.Tagging | ||
| open Microsoft.VisualStudio.Shell | ||
|
|
||
| open Microsoft.FSharp.Compiler.Parser | ||
| open Microsoft.FSharp.Compiler.SourceCodeServices | ||
| open Microsoft.FSharp.Compiler.Range | ||
|
|
||
| [<Shared>] | ||
| [<ExportLanguageServiceFactory(typeof<CompletionService>, FSharpCommonConstants.FSharpLanguageName)>] | ||
| type internal FSharpCompletionServiceFactory [<ImportingConstructor>] (serviceProvider: SVsServiceProvider) = | ||
| interface ILanguageServiceFactory with | ||
| member this.CreateLanguageService(hostLanguageServices: HostLanguageServices) : ILanguageService = | ||
| upcast new FSharpCompletionService(hostLanguageServices.WorkspaceServices.Workspace, serviceProvider) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is sort of a layering violation. Is there no other way to implement this without accessing other VS layer services?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, no, in the existing implementation (see XmlDocumentation.fs). Can you please explain?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the IVsXMLMemberIndexService the only reason for the layer violation? Seems like an abstraction could be added to solve this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The general idea for these providers is that they are not dependent on VS, so they can work outside of VS, either to run out of proc or be used within a different host.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. We can log a future work item for redesigning this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. @OmarTawfik and I chatted about that easier. For now, this is VS-only, and we can factor it differently after this goes in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, that was my only hesitation.