-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 razor completion #35225
Fix razor completion #35225
Changes from 5 commits
f30babb
4a6c40c
52e76c2
d1b15fc
1c17605
7427f14
47138ad
ae12b5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Composition; | ||
using Microsoft.CodeAnalysis.Editor.Shared; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.VisualStudio.Text; | ||
using Microsoft.VisualStudio.InteractiveWindow; | ||
using Microsoft.CodeAnalysis.Shared; | ||
|
||
namespace Microsoft.CodeAnalysis.Editor.Implementation.Interactive | ||
{ | ||
internal sealed class InteractiveSupportsFeatureService | ||
{ | ||
[ExportWorkspaceService(typeof(ITextBufferSupportsFeatureService), WorkspaceKind.Interactive), Shared] | ||
internal class InteractiveTextBufferSupportsFeatureService : ITextBufferSupportsFeatureService | ||
{ | ||
public bool SupportsCodeFixes(ITextBuffer textBuffer) | ||
{ | ||
if (textBuffer != null) | ||
{ | ||
var evaluator = (IInteractiveEvaluator)textBuffer.Properties[typeof(IInteractiveEvaluator)]; | ||
var window = evaluator?.CurrentWindow; | ||
if (window?.CurrentLanguageBuffer == textBuffer) | ||
{ | ||
// These are only correct if we're on the UI thread. | ||
// Otherwise, they're guesses and they might change immediately even if they're correct. | ||
// If we return true and the buffer later becomes readonly, it appears that the | ||
// the code fix simply has no effect. | ||
return !window.IsResetting && !window.IsRunning; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public bool SupportsRefactorings(ITextBuffer textBuffer) | ||
{ | ||
return false; | ||
} | ||
|
||
public bool SupportsRename(ITextBuffer textBuffer) | ||
{ | ||
return false; | ||
} | ||
|
||
public bool SupportsNavigationToAnyPosition(ITextBuffer textBuffer) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
[ExportWorkspaceService(typeof(IDocumentSupportsFeatureService), WorkspaceKind.Interactive), Shared] | ||
internal class InteractiveDocumentSupportsFeatureService : IDocumentSupportsFeatureService | ||
{ | ||
public bool SupportsCodeFixes(Document document) | ||
{ | ||
// TODO: Implement this. | ||
return false; | ||
} | ||
|
||
public bool SupportsRefactorings(Document document) | ||
{ | ||
return false; | ||
} | ||
|
||
public bool SupportsRename(Document document) | ||
{ | ||
return false; | ||
} | ||
|
||
public bool SupportsNavigationToAnyPosition(Document document) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,14 @@ | |
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.AddImports; | ||
using Microsoft.CodeAnalysis.Debugging; | ||
using Microsoft.CodeAnalysis.Editing; | ||
using Microsoft.CodeAnalysis.Experiments; | ||
using Microsoft.CodeAnalysis.Formatting; | ||
using Microsoft.CodeAnalysis.Internal.Log; | ||
using Microsoft.CodeAnalysis.LanguageServices; | ||
using Microsoft.CodeAnalysis.PooledObjects; | ||
using Microsoft.CodeAnalysis.Shared; | ||
using Microsoft.CodeAnalysis.Shared.Extensions; | ||
using Microsoft.CodeAnalysis.Shared.Extensions.ContextQuery; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
@@ -179,7 +181,14 @@ internal override async Task<CompletionChange> GetChangeAsync(Document document, | |
var containingNamespace = TypeImportCompletionItem.GetContainingNamespace(completionItem); | ||
Debug.Assert(containingNamespace != null); | ||
|
||
if (document.Project.Solution.Workspace.CanApplyChange(ApplyChangesKind.ChangeDocument)) | ||
if (ShouldCompleteWithFullyQualifyTypeName(document)) | ||
{ | ||
var fullyQualifiedName = $"{containingNamespace}.{completionItem.DisplayText}"; | ||
var change = new TextChange(completionListSpan, fullyQualifiedName); | ||
|
||
return CompletionChange.Create(change); | ||
} | ||
else | ||
{ | ||
// Find context node so we can use it to decide where to insert using/imports. | ||
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); | ||
|
@@ -223,14 +232,32 @@ internal override async Task<CompletionChange> GetChangeAsync(Document document, | |
|
||
return CompletionChange.Create(Utilities.Collapse(newText, builder.ToImmutableAndFree())); | ||
} | ||
else | ||
|
||
static bool ShouldCompleteWithFullyQualifyTypeName(Document document) | ||
{ | ||
// For workspace that doesn't support document change, e.g. DebuggerIntellisense | ||
// we complete the type name in its fully qualified form instead. | ||
var fullyQualifiedName = $"{containingNamespace}.{completionItem.DisplayText}"; | ||
var change = new TextChange(completionListSpan, fullyQualifiedName); | ||
var workspace = document.Project.Solution.Workspace; | ||
|
||
return CompletionChange.Create(change); | ||
// Certain types of workspace don't support document change, e.g. DebuggerIntellisense | ||
if (!workspace.CanApplyChange(ApplyChangesKind.ChangeDocument)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can use feature service from debugger workspace to disable it rather than using CanApplyChange just for debugger workspace? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me try that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, doesn't work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, you need to export one for DebuggerIntellisenseWorkspace that return false? IDocumentSupportsFeatureService is per workpsace service which supposed to return different value based on workspace? like we have one for interactive and etc? |
||
{ | ||
return true; | ||
} | ||
|
||
// During an EnC session, adding import is not supported. | ||
var encService = workspace.Services.GetService<IDebuggingWorkspaceService>()?.EditAndContinueServiceOpt; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should move to FeatureService ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tmat What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we return "CodeFix and Refactorings not available" in the ITextBufferSupportsFeatureService during EnC? |
||
if (encService?.EditSession != null) | ||
{ | ||
return true; | ||
} | ||
|
||
// Certain documents, e.g. Razor document, don't support adding imports | ||
var documentSupportsFeatureService = workspace.Services.GetService<IDocumentSupportsFeatureService>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmm.. why not add one specific for type import? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the existing |
||
if (!documentSupportsFeatureService.SupportsRefactorings(document)) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Composition; | ||
using Microsoft.CodeAnalysis.Host; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
|
||
namespace Microsoft.CodeAnalysis.Shared | ||
{ | ||
internal interface IDocumentSupportsFeatureService : IWorkspaceService | ||
{ | ||
bool SupportsCodeFixes(Document document); | ||
bool SupportsRefactorings(Document document); | ||
bool SupportsRename(Document document); | ||
bool SupportsNavigationToAnyPosition(Document document); | ||
} | ||
|
||
|
||
[ExportWorkspaceService(typeof(IDocumentSupportsFeatureService), ServiceLayer.Default), Shared] | ||
internal class DefaultDocumentSupportsFeatureService : IDocumentSupportsFeatureService | ||
{ | ||
public bool SupportsCodeFixes(Document document) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we actually need one that accept Document for these? I think @dibarbet actually removed one that accept document and explicitly changed to text buffer for these APIs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't need those for my purpose, they were included just because I wanted to expose the same APIs in Feature layer. @dibarbet Any particular reason that those shouldn't be available for Document? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any reason for them not to be available - it was changed from document -> text buffer because we had the text buffer first in almost all the cases the interface was being used, so we were often just getting the document for no reason. |
||
=> true; | ||
|
||
public bool SupportsNavigationToAnyPosition(Document document) | ||
=> true; | ||
|
||
public bool SupportsRefactorings(Document document) | ||
=> true; | ||
|
||
public bool SupportsRename(Document document) | ||
=> true; | ||
} | ||
} |
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.
you can have 1 type with 2 interfaces with 2 ExportWorkspaceServices?
you can even share instance by exporting ExportWorkspaceServiceFactory with same service instance?
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.
No, you can't. Each export is a nested type itself.