Skip to content

Commit

Permalink
Merge pull request #12099 from Pilchie/CompletionOptions
Browse files Browse the repository at this point in the history
Completion options
  • Loading branch information
Pilchie authored Jun 20, 2016
2 parents df88a0d + c9eb85c commit 033e770
Show file tree
Hide file tree
Showing 30 changed files with 293 additions and 50 deletions.
7 changes: 7 additions & 0 deletions src/EditorFeatures/Core/Extensibility/VersionSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ public static T SelectHighest<T>(IEnumerable<Lazy<T, VisualStudioVersionMetadata
{
return items.OrderByDescending(i => i.Metadata.Version).First().Value;
}

public static T SelectVersion<T>(
IEnumerable<Lazy<T, VisualStudioVersionMetadata>> items,
VisualStudioVersion version)
{
return items.First(i => i.Metadata.Version == version).Value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ internal static class OptionSetExtensions
public static OptionSet WithDebuggerCompletionOptions(this OptionSet options)
{
return options
.WithChangedOption(CompletionOptions.AlwaysShowBuilder, true)
.WithChangedOption(CompletionOptions.FilterOutOfScopeLocals, false)
.WithChangedOption(CompletionOptions.ShowXmlDocCommentCompletion, false);
.WithChangedOption(CompletionControllerOptions.AlwaysShowBuilder, true)
.WithChangedOption(CompletionControllerOptions.FilterOutOfScopeLocals, false)
.WithChangedOption(CompletionControllerOptions.ShowXmlDocCommentCompletion, false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel.Composition;
using System.Linq;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Editor.Extensibility.Composition;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
Expand All @@ -20,7 +25,7 @@ internal sealed class CompletionPresenter : ForegroundThreadAffinitizedObject, I
{
private readonly ICompletionBroker _completionBroker;
private readonly IGlyphService _glyphService;
private readonly ICompletionSetFactory _completionSetFactory;
private readonly ImmutableArray<Lazy<ICompletionSetFactory, VisualStudioVersionMetadata>> _completionSetFactories;

[ImportingConstructor]
public CompletionPresenter(
Expand All @@ -30,14 +35,28 @@ public CompletionPresenter(
{
_completionBroker = completionBroker;
_glyphService = glyphService;
_completionSetFactory = VersionSelector.SelectHighest(completionSetFactories);
_completionSetFactories = completionSetFactories.AsImmutableOrEmpty();
}

ICompletionPresenterSession IIntelliSensePresenter<ICompletionPresenterSession, ICompletionSession>.CreateSession(ITextView textView, ITextBuffer subjectBuffer, ICompletionSession session)
ICompletionPresenterSession IIntelliSensePresenter<ICompletionPresenterSession, ICompletionSession>.CreateSession(
ITextView textView, ITextBuffer subjectBuffer, ICompletionSession session)
{
AssertIsForeground();

var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

var completionSetFactory = document != null && NeedsDev15CompletionSetFactory(document.Options, document.Project.Language)
? VersionSelector.SelectHighest(_completionSetFactories)
: VersionSelector.SelectVersion(_completionSetFactories, VisualStudioVersion.Dev14);

return new CompletionPresenterSession(
_completionSetFactory, _completionBroker, _glyphService, textView, subjectBuffer);
completionSetFactory, _completionBroker, _glyphService, textView, subjectBuffer);
}

private bool NeedsDev15CompletionSetFactory(OptionSet options, string language)
{
return CompletionOptions.GetDev15CompletionOptions().Any(
o => options.GetOption(o, language));
}

ICompletionSource ICompletionSourceProvider.TryCreateCompletionSource(ITextBuffer textBuffer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,14 @@ public PresentationItem GetPresentationItem(VSCompletion completion)
return null;
}

protected string GetLanguage()
protected Document GetDocument()
{
var document = SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document != null)
{
return document.Project.Language;
}
return SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();
}

return "";
protected string GetLanguage()
{
return GetDocument()?.Project.Language ?? "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,33 @@ internal class Roslyn15CompletionSet : Roslyn14CompletionSet
private CompletionHelper _completionHelper;
public IReadOnlyList<IntellisenseFilter2> Filters;

private readonly bool _highlightMatchingPortions;
private readonly bool _showFilters;

public Roslyn15CompletionSet(
IVisualStudioCompletionSet vsCompletionSet,
CompletionPresenterSession completionPresenterSession,
ITextView textView,
ITextBuffer subjectBuffer)
: base(vsCompletionSet, completionPresenterSession, textView, subjectBuffer)
{
var document = GetDocument();

if (document != null)
{
var options = document.Options;
_highlightMatchingPortions = options.GetOption(CompletionOptions.HighlightMatchingPortionsOfCompletionListItems);
_showFilters = options.GetOption(CompletionOptions.ShowCompletionItemFilters);
}
}

protected override void SetupFilters(ImmutableArray<CompletionItemFilter> completionItemFilters)
{
// If more than one filter was provided, then present it to the user.
if (Filters == null && completionItemFilters.Length > 1)
if (_showFilters && Filters == null && completionItemFilters.Length > 1)
{
Filters = completionItemFilters.Select(f => new IntellisenseFilter2(this, f, GetLanguage()))
.ToArray();
.ToArray();
}
}

Expand All @@ -48,7 +59,7 @@ private CompletionHelper GetCompletionHelper()
this.AssertIsForeground();
if (_completionHelper == null)
{
var document = SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();
var document = GetDocument();
if (document != null)
{
_completionHelper = CompletionHelper.GetHelper(document,
Expand All @@ -61,7 +72,7 @@ private CompletionHelper GetCompletionHelper()

public IReadOnlyList<Span> GetHighlightedSpansInDisplayText(string displayText)
{
if (CompletionItemToFilterText != null)
if (_highlightMatchingPortions && CompletionItemToFilterText != null)
{
var completionHelper = this.GetCompletionHelper();
if (completionHelper != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal static bool IsTriggerAfterSpaceOrStartOfWordCharacter(SourceText text,
// Bring up on space or at the start of a word.
var ch = text[characterPosition];
return SpaceTypedNotBeforeWord(ch, text, characterPosition) ||
(CompletionUtilities.IsStartingNewWord(text, characterPosition) && options.GetOption(CompletionOptions.TriggerOnTypingLetters, LanguageNames.CSharp));
(IsStartingNewWord(text, characterPosition) && options.GetOption(CompletionOptions.TriggerOnTypingLetters, LanguageNames.CSharp));
}

private static bool SpaceTypedNotBeforeWord(char ch, SourceText text, int characterPosition)
Expand Down
19 changes: 17 additions & 2 deletions src/Features/Core/Portable/Completion/CompletionOptions.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
// 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.Collections.Generic;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.Completion
{
internal static class CompletionOptions
{
internal const string FeatureName = "Completion";
internal const string ControllerFeatureName = "CompletionController";

public static readonly PerLanguageOption<bool> HideAdvancedMembers = new PerLanguageOption<bool>(FeatureName, "HideAdvancedMembers", defaultValue: false);
public static readonly PerLanguageOption<bool> IncludeKeywords = new PerLanguageOption<bool>(FeatureName, "IncludeKeywords", defaultValue: true);
public static readonly PerLanguageOption<bool> TriggerOnTyping = new PerLanguageOption<bool>(FeatureName, "TriggerOnTyping", defaultValue: true);
public static readonly PerLanguageOption<bool> TriggerOnTypingLetters = new PerLanguageOption<bool>(FeatureName, "TriggerOnTypingLetters", defaultValue: true);

// Dev15 options
public static readonly PerLanguageOption<bool> ShowCompletionItemFilters = new PerLanguageOption<bool>(FeatureName, nameof(ShowCompletionItemFilters), defaultValue: false);
public static readonly PerLanguageOption<bool> HighlightMatchingPortionsOfCompletionListItems = new PerLanguageOption<bool>(FeatureName, nameof(HighlightMatchingPortionsOfCompletionListItems), defaultValue: false);

public static IEnumerable<PerLanguageOption<bool>> GetDev15CompletionOptions()
{
yield return ShowCompletionItemFilters;
yield return HighlightMatchingPortionsOfCompletionListItems;
}
}

internal static class CompletionControllerOptions
{
internal const string ControllerFeatureName = "CompletionController";

public static readonly Option<bool> AlwaysShowBuilder = new Option<bool>(ControllerFeatureName, "AlwaysShowBuilder", defaultValue: false);
public static readonly Option<bool> FilterOutOfScopeLocals = new Option<bool>(ControllerFeatureName, "FilterOutOfScopeLocals", defaultValue: true);
public static readonly Option<bool> ShowXmlDocCommentCompletion = new Option<bool>(ControllerFeatureName, "ShowXmlDocCommentCompletion", defaultValue: true);
}
}
}
21 changes: 9 additions & 12 deletions src/Features/Core/Portable/Completion/CompletionOptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ namespace Microsoft.CodeAnalysis.Completion
[ExportOptionProvider, Shared]
internal class CompletionOptionsProvider : IOptionProvider
{
private readonly IEnumerable<IOption> _options = new List<IOption>
{
CompletionOptions.HideAdvancedMembers,
CompletionOptions.IncludeKeywords,
CompletionOptions.TriggerOnTyping,
CompletionOptions.TriggerOnTypingLetters
}.ToImmutableArray();
private readonly IEnumerable<IOption> _options = ImmutableArray.Create<IOption>(
CompletionOptions.HideAdvancedMembers,
CompletionOptions.IncludeKeywords,
CompletionOptions.TriggerOnTyping,
CompletionOptions.TriggerOnTypingLetters,
CompletionOptions.ShowCompletionItemFilters,
CompletionOptions.HighlightMatchingPortionsOfCompletionListItems);

public IEnumerable<IOption> GetOptions()
{
return _options;
}
public IEnumerable<IOption> GetOptions() => _options;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal abstract class AbstractDocCommentCompletionProvider : CommonCompletionP

public override async Task ProvideCompletionsAsync(CompletionContext context)
{
if (!context.Options.GetOption(CompletionOptions.ShowXmlDocCommentCompletion))
if (!context.Options.GetOption(CompletionControllerOptions.ShowXmlDocCommentCompletion))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private bool IsCandidateProject(AbstractSyntaxContext context, CancellationToken

protected OptionSet GetUpdatedRecommendationOptions(OptionSet options, string language)
{
var filterOutOfScopeLocals = options.GetOption(CompletionOptions.FilterOutOfScopeLocals);
var filterOutOfScopeLocals = options.GetOption(CompletionControllerOptions.FilterOutOfScopeLocals);
var hideAdvancedMembers = options.GetOption(CompletionOptions.HideAdvancedMembers, language);

return options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal abstract class SuggestionModeCompletionProvider : CommonCompletionProvi

public override async Task ProvideCompletionsAsync(CompletionContext context)
{
if (context.Options.GetOption(CompletionOptions.AlwaysShowBuilder))
if (context.Options.GetOption(CompletionControllerOptions.AlwaysShowBuilder))
{
var text = await context.Document.GetTextAsync(context.CancellationToken).ConfigureAwait(false);
context.SuggestionModeItem = this.CreateEmptySuggestionModeItem(context.DefaultItemSpan);
Expand Down
18 changes: 18 additions & 0 deletions src/VisualStudio/CSharp/Impl/CSharpVSResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/VisualStudio/CSharp/Impl/CSharpVSResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,10 @@
<data name="Option_Split_string_literals_on_enter" xml:space="preserve">
<value>Split string literals on _enter</value>
</data>
<data name="Option_Highlight_matching_portions_of_completion_list_items" xml:space="preserve">
<value>_Highlight matching portions of completion list items</value>
</data>
<data name="Option_Show_completion_item_filters" xml:space="preserve">
<value>Show completion item _filters</value>
</data>
</root>
12 changes: 12 additions & 0 deletions src/VisualStudio/CSharp/Impl/Options/AutomationObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public int BringUpOnIdentifier
set { SetBooleanOption(CompletionOptions.TriggerOnTypingLetters, value); }
}

public int HighlightMatchingPortionsOfCompletionListItems
{
get { return GetBooleanOption(CompletionOptions.HighlightMatchingPortionsOfCompletionListItems); }
set { SetBooleanOption(CompletionOptions.HighlightMatchingPortionsOfCompletionListItems, value); }
}

public int ShowCompletionItemFilters
{
get { return GetBooleanOption(CompletionOptions.ShowCompletionItemFilters); }
set { SetBooleanOption(CompletionOptions.ShowCompletionItemFilters, value); }
}

[Obsolete("This SettingStore option has now been deprecated in favor of CSharpClosedFileDiagnostics")]
public int ClosedFileDiagnostics
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@

namespace Microsoft.VisualStudio.LanguageServices.CSharp.Options
{
[ExportLanguageSpecificOptionSerializer(LanguageNames.CSharp, FormattingOptions.TabFeatureName, BraceCompletionOptions.FeatureName, CompletionOptions.FeatureName, SignatureHelpOptions.FeatureName, NavigationBarOptions.FeatureName), Shared]
[ExportLanguageSpecificOptionSerializer(
LanguageNames.CSharp,
FormattingOptions.TabFeatureName,
BraceCompletionOptions.FeatureName,
CompletionOptions.FeatureName,
SignatureHelpOptions.FeatureName,
NavigationBarOptions.FeatureName), Shared]
internal class CSharpLanguageSettingsSerializer : AbstractLanguageSettingsSerializer
{
[ImportingConstructor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ protected override ImmutableDictionary<string, IOption> CreateStorageKeyToOption
{
new KeyValuePair<string, IOption>(GetStorageKeyForOption(CompletionOptions.IncludeKeywords), CompletionOptions.IncludeKeywords),
new KeyValuePair<string, IOption>(GetStorageKeyForOption(CompletionOptions.TriggerOnTypingLetters), CompletionOptions.TriggerOnTypingLetters),
new KeyValuePair<string, IOption>(GetStorageKeyForOption(CompletionOptions.ShowCompletionItemFilters), CompletionOptions.ShowCompletionItemFilters),
new KeyValuePair<string, IOption>(GetStorageKeyForOption(CompletionOptions.HighlightMatchingPortionsOfCompletionListItems), CompletionOptions.HighlightMatchingPortionsOfCompletionListItems),
});

Type[] types = new[]
Expand Down Expand Up @@ -145,6 +147,8 @@ protected override bool SupportsOption(IOption option, string languageName)
{
if (option == CompletionOptions.IncludeKeywords ||
option == CompletionOptions.TriggerOnTypingLetters ||
option == CompletionOptions.ShowCompletionItemFilters ||
option == CompletionOptions.HighlightMatchingPortionsOfCompletionListItems ||
option.Feature == SimplificationOptions.PerLanguageFeatureName ||
option.Feature == ExtractMethodOptions.FeatureName ||
option.Feature == ServiceFeatureOnOffOptions.OptionName ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@
x:Name="ShowSnippets"
Content="{x:Static local:IntelliSenseOptionPageStrings.Option_ShowSnippets}" />
</StackPanel>
</StackPanel>
</GroupBox>
<GroupBox x:Uid="SelectionInCompletionListGroupBox"
Header="{x:Static local:IntelliSenseOptionPageStrings.Option_SelectionInCompletionList}">
<StackPanel>

<CheckBox x:Uid="InsertNewlineOnEnterWithWholeWord"
x:Name="InsertNewlineOnEnterWithWholeWord"
Content="{x:Static local:IntelliSenseOptionPageStrings.Option_InsertNewlineOnEnterWithWholeWord}" />
x:Name="InsertNewlineOnEnterWithWholeWord"
Content="{x:Static local:IntelliSenseOptionPageStrings.Option_InsertNewlineOnEnterWithWholeWord}" />
<CheckBox x:Uid="Highlight_matching_portions_of_completion_list_items"
x:Name="Highlight_matching_portions_of_completion_list_items"
Content="{x:Static local:IntelliSenseOptionPageStrings.Option_Highlight_matching_portions_of_completion_list_items}" />
<CheckBox x:Uid="Show_completion_item_filters"
x:Name="Show_completion_item_filters"
Content="{x:Static local:IntelliSenseOptionPageStrings.Option_Show_completion_item_filters}" />

</StackPanel>
</GroupBox>
</StackPanel>
Expand Down
Loading

0 comments on commit 033e770

Please sign in to comment.