Skip to content

Commit

Permalink
Merge pull request #11080 from jasonmalinowski/implement-document-and…
Browse files Browse the repository at this point in the history
…-project-specific-options

Add API for consuming solution and document-specific options
  • Loading branch information
jasonmalinowski committed May 10, 2016
2 parents e4f7a4a + c9d91a9 commit 5f95f2d
Show file tree
Hide file tree
Showing 119 changed files with 481 additions and 550 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public CSharpEditorFormattingService()

public bool SupportsFormattingOnTypedCharacter(Document document, char ch)
{
var options = document.Project.Solution.Workspace.Options;
var smartIndentOn = options.GetOption(FormattingOptions.SmartIndent, document.Project.Language) == FormattingOptions.IndentStyle.Smart;
var options = document.Options;
var smartIndentOn = options.GetOption(FormattingOptions.SmartIndent) == FormattingOptions.IndentStyle.Smart;

if ((ch == '}' && !options.GetOption(FeatureOnOffOptions.AutoFormattingOnCloseBrace, document.Project.Language) && !smartIndentOn) ||
(ch == ';' && !options.GetOption(FeatureOnOffOptions.AutoFormattingOnSemicolon, document.Project.Language)))
if ((ch == '}' && !options.GetOption(FeatureOnOffOptions.AutoFormattingOnCloseBrace) && !smartIndentOn) ||
(ch == ';' && !options.GetOption(FeatureOnOffOptions.AutoFormattingOnSemicolon)))
{
return false;
}
Expand Down Expand Up @@ -182,12 +182,10 @@ public async Task<IList<TextChange>> GetFormattingChangesAsync(Document document
return null;
}

var options = document.Project.Solution.Workspace.Options;

// don't attempt to format on close brace if autoformat on close brace feature is off, instead just smart indent
bool smartIndentOnly =
token.IsKind(SyntaxKind.CloseBraceToken) &&
!options.GetOption(FeatureOnOffOptions.AutoFormattingOnCloseBrace, document.Project.Language);
!document.Options.GetOption(FeatureOnOffOptions.AutoFormattingOnCloseBrace);

if (!smartIndentOnly)
{
Expand Down Expand Up @@ -216,7 +214,7 @@ private static async Task<SyntaxToken> GetTokenBeforeTheCaretAsync(Document docu
private async Task<IList<TextChange>> FormatTokenAsync(Document document, SyntaxToken token, IEnumerable<IFormattingRule> formattingRules, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var formatter = CreateSmartTokenFormatter(document.Project.Solution.Workspace.Options, formattingRules, root);
var formatter = CreateSmartTokenFormatter(document.Options, formattingRules, root);
var changes = await formatter.FormatTokenAsync(document.Project.Solution.Workspace, token, cancellationToken).ConfigureAwait(false);
return changes;
}
Expand Down Expand Up @@ -247,7 +245,7 @@ private ISmartTokenFormatter CreateSmartTokenFormatter(OptionSet optionSet, IEnu
}

var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var formatter = new SmartTokenFormatter(document.Project.Solution.Workspace.Options, formattingRules, (CompilationUnitSyntax)root);
var formatter = new SmartTokenFormatter(document.Options, formattingRules, (CompilationUnitSyntax)root);

var changes = formatter.FormatRange(document.Project.Solution.Workspace, tokenRange.Value.Item1, tokenRange.Value.Item2, cancellationToken);
return changes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Task<IList<TextChange>> FormatTokenAsync(Workspace workspace, SyntaxToken

var smartTokenformattingRules = (new SmartTokenFormattingRule()).Concat(_formattingRules);
var adjustedStartPosition = previousToken.SpanStart;
var indentStyle = workspace.Options.GetOption(FormattingOptions.SmartIndent, LanguageNames.CSharp);
var indentStyle = _optionSet.GetOption(FormattingOptions.SmartIndent, LanguageNames.CSharp);
if (token.IsKind(SyntaxKind.OpenBraceToken) && token.IsFirstTokenOnLine(token.SyntaxTree.GetText()) && indentStyle != FormattingOptions.IndentStyle.Smart)
{
adjustedStartPosition = token.SpanStart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ protected async Task VerifySendEnterThroughToEnterAsync(string initialMarkup, st
var completionList = await GetCompletionListAsync(document, position, CompletionTrigger.Default);
var item = completionList.Items.First(i => i.DisplayText.StartsWith(textTypedSoFar));

var optionService = workspace.Services.GetService<IOptionService>();
var options = optionService.GetOptions().WithChangedOption(CSharpCompletionOptions.AddNewLineOnEnterAfterFullyTypedWord, sendThroughEnterEnabled);
optionService.SetOptions(options);
workspace.Options = workspace.Options.WithChangedOption(CSharpCompletionOptions.AddNewLineOnEnterAfterFullyTypedWord, sendThroughEnterEnabled);

var completionRules = CompletionHelper.GetHelper(document);
Assert.Equal(expected, completionRules.SendEnterThroughToEditor(item, textTypedSoFar, workspace.Options));
Expand Down
11 changes: 4 additions & 7 deletions src/EditorFeatures/CSharpTest/ExtractMethod/ExtractMethodBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ protected async Task NotSupported_ExtractMethodAsync(string codeWithMarker)
var document = workspace.CurrentSolution.GetDocument(testDocument.Id);
Assert.NotNull(document);

var options = document.Project.Solution.Workspace.Options
.WithChangedOption(ExtractMethodOptions.AllowMovingDeclaration, document.Project.Language, allowMovingDeclaration)
.WithChangedOption(ExtractMethodOptions.DontPutOutOrRefOnStruct, document.Project.Language, dontPutOutOrRefOnStruct);
var options = document.Options.WithChangedOption(ExtractMethodOptions.AllowMovingDeclaration, document.Project.Language, allowMovingDeclaration)
.WithChangedOption(ExtractMethodOptions.DontPutOutOrRefOnStruct, document.Project.Language, dontPutOutOrRefOnStruct);

var semanticDocument = await SemanticDocument.CreateAsync(document, CancellationToken.None);
var validator = new CSharpSelectionValidator(semanticDocument, testDocument.SelectedSpans.Single(), options);
Expand Down Expand Up @@ -145,8 +144,7 @@ protected async Task TestSelectionAsync(string codeWithMarker, bool expectedFail
var document = workspace.CurrentSolution.GetDocument(testDocument.Id);
Assert.NotNull(document);

var options = document.Project.Solution.Workspace.Options
.WithChangedOption(ExtractMethodOptions.AllowMovingDeclaration, document.Project.Language, true);
var options = document.Options.WithChangedOption(ExtractMethodOptions.AllowMovingDeclaration, document.Project.Language, true);

var semanticDocument = await SemanticDocument.CreateAsync(document, CancellationToken.None);
var validator = new CSharpSelectionValidator(semanticDocument, namedSpans["b"].Single(), options);
Expand All @@ -172,8 +170,7 @@ protected async Task IterateAllAsync(string code)
var root = await document.GetSyntaxRootAsync();
var iterator = root.DescendantNodesAndSelf().Cast<SyntaxNode>();

var options = document.Project.Solution.Workspace.Options
.WithChangedOption(ExtractMethodOptions.AllowMovingDeclaration, document.Project.Language, true);
var options = document.Options.WithChangedOption(ExtractMethodOptions.AllowMovingDeclaration, document.Project.Language, true);

foreach (var node in iterator)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,8 @@ protected static async Task AssertFormatWithPasteOrReturnAsync(string expectedWi
// get original buffer
var buffer = workspace.Documents.First().GetTextBuffer();

var optionService = workspace.Services.GetService<IOptionService>();
if (isPaste)
{
optionService.SetOptions(optionService.GetOptions().WithChangedOption(FeatureOnOffOptions.FormatOnPaste, LanguageNames.CSharp, true));
var commandHandler = new FormatCommandHandler(TestWaitIndicator.Default, null, null);
var commandArgs = new PasteCommandArgs(view, view.TextBuffer);
commandHandler.ExecuteCommand(commandArgs, () => { });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static async Task TokenFormatWorkerAsync(TestWorkspace workspace, ITextB

var rules = formattingRuleProvider.CreateRule(document, position).Concat(Formatter.GetDefaultFormattingRules(document));

var formatter = new SmartTokenFormatter(workspace.Options, rules, root);
var formatter = new SmartTokenFormatter(document.Options, rules, root);
var changes = await formatter.FormatTokenAsync(workspace, token, CancellationToken.None);

ApplyChanges(buffer, changes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ void Main(object o)
Assert.True(
CSharpIndentationService.ShouldUseSmartTokenFormatterInsteadOfIndenter(
Formatter.GetDefaultFormattingRules(workspace, root.Language),
root, line, workspace.Options, CancellationToken.None));
root, line, document.Options, CancellationToken.None));

var actualIndentation = await GetSmartTokenFormatterIndentationWorkerAsync(workspace, buffer, indentationLine, ch);
Assert.Equal(expectedIndentation.Value, actualIndentation);
Expand All @@ -1392,7 +1392,7 @@ void Main(object o)
Assert.False(
CSharpIndentationService.ShouldUseSmartTokenFormatterInsteadOfIndenter(
Formatter.GetDefaultFormattingRules(workspace, root.Language),
root, line, workspace.Options, CancellationToken.None));
root, line, document.Options, CancellationToken.None));

await TestIndentationAsync(indentationLine, expectedIndentation, workspace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3093,7 +3093,6 @@ internal static async Task AutoFormatTokenAsync(string markup, string expected)
{
var subjectDocument = workspace.Documents.Single();

var optionService = workspace.Services.GetService<IOptionService>();
var textUndoHistory = new Mock<ITextUndoHistoryRegistry>();
var editorOperationsFactory = new Mock<IEditorOperationsFactoryService>();
var editorOperations = new Mock<IEditorOperations>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void ExecuteCommand(AutomaticLineEnderCommandArgs args, Action nextHandle
}

// feature off
if (!document.Project.Solution.Workspace.Options.GetOption(InternalFeatureOnOffOptions.AutomaticLineEnder))
if (!document.Options.GetOption(InternalFeatureOnOffOptions.AutomaticLineEnder))
{
NextAction(operations, nextHandler);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void NavigateTo()
if (document != null)
{
var navigator = _workspace.Services.GetService<IDocumentNavigationService>();
var options = _workspace.Options.WithChangedOption(NavigationOptions.PreferProvisionalTab, true);
var options = document.Options.WithChangedOption(NavigationOptions.PreferProvisionalTab, true);
navigator.TryNavigateToSpan(_workspace, document.Id, _span, options);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,14 @@ public void ExecuteCommand(ExtractMethodCommandArgs args, Action nextHandler)
return false;
}

var options = document.Project.Solution.Workspace.Options;
var result = ExtractMethodService.ExtractMethodAsync(
document, spans.Single().Span.ToTextSpan(), options, cancellationToken).WaitAndGetResult(cancellationToken);
document, spans.Single().Span.ToTextSpan(), cancellationToken: cancellationToken).WaitAndGetResult(cancellationToken);
Contract.ThrowIfNull(result);

if (!result.Succeeded && !result.SucceededWithSuggestion)
{
// if it failed due to out/ref parameter in async method, try it with different option
var newResult = TryWithoutMakingValueTypesRef(document, spans, options, result, cancellationToken);
var newResult = TryWithoutMakingValueTypesRef(document, spans, result, cancellationToken);
if (newResult != null)
{
var notificationService = document.Project.Solution.Workspace.Services.GetService<INotificationService>();
Expand Down Expand Up @@ -191,7 +190,7 @@ private bool TryNotifyFailureToUser(Document document, ExtractMethodResult resul
var notificationService = document.Project.Solution.Workspace.Services.GetService<INotificationService>();

// see whether we will allow best effort extraction and if it is possible.
if (!document.Project.Solution.Workspace.Options.GetOption(ExtractMethodOptions.AllowBestEffort, document.Project.Language) ||
if (!document.Options.GetOption(ExtractMethodOptions.AllowBestEffort) ||
!result.Status.HasBestEffort() || result.Document == null)
{
if (notificationService != null)
Expand Down Expand Up @@ -224,8 +223,10 @@ private bool TryNotifyFailureToUser(Document document, ExtractMethodResult resul
}

private static ExtractMethodResult TryWithoutMakingValueTypesRef(
Document document, NormalizedSnapshotSpanCollection spans, OptionSet options, ExtractMethodResult result, CancellationToken cancellationToken)
Document document, NormalizedSnapshotSpanCollection spans, ExtractMethodResult result, CancellationToken cancellationToken)
{
OptionSet options = document.Options;

if (options.GetOption(ExtractMethodOptions.DontPutOutOrRefOnStruct, document.Project.Language) || !result.Reasons.IsSingle())
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal abstract class AbstractSmartTokenFormatterCommandHandler :
protected bool FormatToken(ITextView view, Document document, SyntaxToken token, IEnumerable<IFormattingRule> formattingRules, CancellationToken cancellationToken)
{
var root = document.GetSyntaxRootAsync(cancellationToken).WaitAndGetResult(cancellationToken);
var formatter = CreateSmartTokenFormatter(document.Project.Solution.Workspace.Options, formattingRules, root);
var formatter = CreateSmartTokenFormatter(document.Options, formattingRules, root);
var changes = formatter.FormatTokenAsync(document.Project.Solution.Workspace, token, cancellationToken).WaitAndGetResult(cancellationToken);
if (changes.Count == 0)
{
Expand Down Expand Up @@ -226,14 +226,14 @@ private static void ReadjustIndentation(ITextView view, ITextBuffer subjectBuffe
return;
}

var options = document.Project.Solution.Workspace.Options;
var options = document.Options;

// and then, insert the text
document.Project.Solution.Workspace.ApplyTextChanges(document.Id,
new TextChange(
new TextSpan(
lineInSubjectBuffer.Start.Position, firstNonWhitespaceIndex),
indentation.CreateIndentationString(options.GetOption(FormattingOptions.UseTabs, document.Project.Language), options.GetOption(FormattingOptions.TabSize, document.Project.Language))),
indentation.CreateIndentationString(options.GetOption(FormattingOptions.UseTabs), options.GetOption(FormattingOptions.TabSize))),
CancellationToken.None);
}

Expand Down Expand Up @@ -296,7 +296,7 @@ private bool TryFormatUsingTokenFormatter(ITextView view, ITextBuffer subjectBuf
var position = view.GetCaretPoint(subjectBuffer).Value;
var line = position.GetContainingLine();
var root = document.GetSyntaxRootAsync(cancellationToken).WaitAndGetResult(cancellationToken);
var options = document.Project.Solution.Workspace.Options;
var options = document.Options;
if (!UseSmartTokenFormatter(root, line, formattingRules, options, cancellationToken))
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal static class GoToDefinitionHelpers
symbol = ((IMethodSymbol)symbol).PartialImplementationPart ?? symbol;
}

var options = project.Solution.Workspace.Options;
var options = project.Solution.Options;

var preferredSourceLocations = NavigableItemFactory.GetPreferredSourceLocations(solution, symbol).ToArray();
var title = NavigableItemFactory.GetSymbolDisplayString(project, symbol);
Expand All @@ -76,7 +76,7 @@ internal static class GoToDefinitionHelpers
var externalSourceDefinitions = FindExternalDefinitionsAsync(symbol, project, externalDefinitionProviders, cancellationToken).WaitAndGetResult(cancellationToken).ToImmutableArray();
if (externalSourceDefinitions.Length > 0)
{
return TryGoToDefinition(externalSourceDefinitions, title, project.Solution.Workspace.Options, presenters, throwOnHiddenDefinition);
return TryGoToDefinition(externalSourceDefinitions, title, options, presenters, throwOnHiddenDefinition);
}
}

Expand All @@ -93,7 +93,7 @@ internal static class GoToDefinitionHelpers
}

var navigableItems = preferredSourceLocations.Select(location => NavigableItemFactory.GetItemFromSymbolLocation(solution, symbol, location)).ToImmutableArray();
return TryGoToDefinition(navigableItems, title, project.Solution.Workspace.Options, presenters, throwOnHiddenDefinition);
return TryGoToDefinition(navigableItems, title, options, presenters, throwOnHiddenDefinition);
}

private static bool TryGoToDefinition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,16 @@ void ICommandHandler<ToggleCompletionModeCommandArgs>.ExecuteCommand(ToggleCompl

if (Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out workspace))
{
var optionService = workspace.Services.GetService<IOptionService>();
var optionSet = optionService.GetOptions();

var wasEnabled = optionService.GetOption(EditorCompletionOptions.UseSuggestionMode);
optionService.SetOptions(optionSet.WithChangedOption(EditorCompletionOptions.UseSuggestionMode, !wasEnabled));
var newState = !workspace.Options.GetOption(EditorCompletionOptions.UseSuggestionMode);
workspace.Options = workspace.Options.WithChangedOption(EditorCompletionOptions.UseSuggestionMode, newState);

// If we don't have a computation in progress, then we don't have to do anything here.
if (this.sessionOpt == null)
{
return;
}

this.sessionOpt.SetModelBuilderState(!wasEnabled);
this.sessionOpt.SetModelBuilderState(newState);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ protected override async Task ProduceTagsAsync(TaggerContext<KeywordHighlightTag
return;
}

var options = document.Project.Solution.Workspace.Options;
if (!options.GetOption(FeatureOnOffOptions.KeywordHighlighting, document.Project.Language))
if (!document.Options.GetOption(FeatureOnOffOptions.KeywordHighlighting))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ protected override async Task ProduceTagsAsync(TaggerContext<LineSeparatorTag> c
return;
}

var options = document.Project.Solution.Workspace.Options;
if (!options.GetOption(FeatureOnOffOptions.LineSeparator, document.Project.Language))
if (!document.Options.GetOption(FeatureOnOffOptions.LineSeparator))
{
return;
}
Expand Down
Loading

0 comments on commit 5f95f2d

Please sign in to comment.