protected override void Run(ITextEditor textEditor, RefactoringProvider provider) { if (textEditor.SelectionLength > 0) { MethodExtractorBase extractor = GetCurrentExtractor(textEditor); if (extractor != null) { if (extractor.Extract()) { ExtractMethodForm form = new ExtractMethodForm(extractor.ExtractedMethod, new Func <IOutputAstVisitor>(extractor.GetOutputVisitor)); if (form.ShowDialog() == DialogResult.OK) { extractor.ExtractedMethod.Name = form.Text; using (textEditor.Document.OpenUndoGroup()) { extractor.InsertAfterCurrentMethod(); extractor.InsertCall(); textEditor.Language.FormattingStrategy.IndentLines(textEditor, 0, textEditor.Document.TotalNumberOfLines - 1); } textEditor.Select(textEditor.SelectionStart, 0); } } } } }
protected override void Run(ITextEditor textEditor, RefactoringProvider provider) { ResolveResult rr = ResolveAtCaret(textEditor); if (rr is MixedResolveResult) { rr = (rr as MixedResolveResult).PrimaryResult; } if (rr is TypeResolveResult) { Rename((rr as TypeResolveResult).ResolvedClass); } else if (rr is MemberResolveResult) { Rename((rr as MemberResolveResult).ResolvedMember); } else if (rr is MethodGroupResolveResult) { Rename((rr as MethodGroupResolveResult).GetMethodIfSingleOverload()); } else if (rr is LocalResolveResult) { RenameLocalVariableCommand.Run(rr as LocalResolveResult); } else { ShowUnknownSymbolError(); } }
public override void Run() { if (ParserService.LoadSolutionProjectsThreadRunning) { return; } if (WorkbenchSingleton.Workbench == null) { return; } ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider; if (provider == null) { return; } LanguageProperties language = ParserService.CurrentProjectContent.Language; if (language == null) { return; } RefactoringProvider rp = language.RefactoringProvider; Run(provider.TextEditor, rp); }
internal async Task VerifyRefactoringAsync( string source, string expected, TextSpan span, IEnumerable <string> additionalSources = null, string title = null, string equivalenceKey = null, CodeVerificationOptions options = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); options ??= Options; using (Workspace workspace = new AdhocWorkspace()) { Document document = WorkspaceFactory.CreateDocument(workspace.CurrentSolution, source, additionalSources, options); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken); ImmutableArray <Diagnostic> compilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); VerifyCompilerDiagnostics(compilerDiagnostics, options); CodeAction action = null; var context = new CodeRefactoringContext( document, span, a => { if (equivalenceKey == null || string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal)) { if (action == null) { action = a; } } }, CancellationToken.None); await RefactoringProvider.ComputeRefactoringsAsync(context); Assert.True(action != null, "No code refactoring has been registered."); document = await VerifyAndApplyCodeActionAsync(document, action, title); semanticModel = await document.GetSemanticModelAsync(cancellationToken); ImmutableArray <Diagnostic> newCompilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); VerifyCompilerDiagnostics(newCompilerDiagnostics, options); VerifyNoNewCompilerDiagnostics(compilerDiagnostics, newCompilerDiagnostics, options); string actual = await document.ToFullStringAsync(simplify : true, format : true, cancellationToken); Assert.Equal(expected, actual); } }
public void TestAllActionsCovered(RefactoringProvider provider, string caseName, string sourcePath, IEnumerable <string> actionPaths) { var source = File.ReadAllText(sourcePath); var actionTitles = actionPaths.Select(GetActionTitle); var cursorPos = source.IndexOf(CursorSymbol); AreNotEqual(-1, cursorPos, "There are no cursor in test source"); source = source.Replace(CursorSymbol, ""); using (var workspace = GetWorkspace()) { var project = workspace.AddProject("TestProject", CSharp) .AddMetadataReference(corlibReference) .AddMetadataReference(s_systemCoreReference); var document = project.AddDocument("TestCode.cs", source); var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); var actions = new List <CodeAction>(); var context = new CodeRefactoringContext( document: document, span: TextSpan.FromBounds(start: cursorPos, end: cursorPos), registerRefactoring: a => { actions.Add(a); }, cancellationToken: cts.Token); provider.Provider.ComputeRefactoringsAsync(context).Wait(); var notPresentActions = actions.Select(a => a.Title).Except(actionTitles).ToList(); CollectionAssert.IsEmpty(notPresentActions, "Provider: {0}, Case: {1}", provider.GetType().Name, caseName); } }
public async Task VerifyNoRefactoringAsync( string source, IEnumerable <TextSpan> spans, string equivalenceKey, CancellationToken cancellationToken = default(CancellationToken)) { Document document = CreateDocument(source); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); ImmutableArray <Diagnostic> compilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); VerifyCompilerDiagnostics(compilerDiagnostics); foreach (TextSpan span in spans) { var context = new CodeRefactoringContext( document, span, a => { if (equivalenceKey == null || string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal)) { Assert.True(false, "Expected no code refactoring."); } }, CancellationToken.None); await RefactoringProvider.ComputeRefactoringsAsync(context).ConfigureAwait(false); } }
private static string GetProviderDirectory(RefactoringProvider provider) { var dirName = provider.ToString(); var providerDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Refactorings", dirName); return(providerDirectory); }
protected override void Run(ITextEditor textEditor, RefactoringProvider provider) { new Snippet { Elements = { new InlineRefactorSnippetElement(context => CreateProperties.CreateDialog(context), "") } }.Insert((TextArea)textEditor.GetService(typeof(TextArea))); }
public async Task VerifyNoRefactoringAsync( string source, IEnumerable <TextSpan> spans, string equivalenceKey = null, CodeVerificationOptions options = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); using (Workspace workspace = new AdhocWorkspace()) { Project project = WorkspaceFactory.AddProject(workspace.CurrentSolution, options); Document document = WorkspaceFactory.AddDocument(project, source); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); ImmutableArray <Diagnostic> compilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); if (options == null) { options = Options; } VerifyCompilerDiagnostics(compilerDiagnostics, options); using (IEnumerator <TextSpan> en = spans.GetEnumerator()) { if (!en.MoveNext()) { throw new InvalidOperationException($"'{nameof(spans)}' contains no elements."); } do { cancellationToken.ThrowIfCancellationRequested(); var context = new CodeRefactoringContext( document, en.Current, a => { if (equivalenceKey == null || string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal)) { Assert.True(false, "No code refactoring expected."); } }, CancellationToken.None); await RefactoringProvider.ComputeRefactoringsAsync(context).ConfigureAwait(false); } while (en.MoveNext()); } } }
public async Task VerifyRefactoringAsync( string source, string expected, TextSpan span, string equivalenceKey, string[] additionalSources = null, CancellationToken cancellationToken = default(CancellationToken)) { Document document = CreateDocument(source, additionalSources ?? Array.Empty <string>()); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); ImmutableArray <Diagnostic> compilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); VerifyCompilerDiagnostics(compilerDiagnostics); CodeAction action = null; var context = new CodeRefactoringContext( document, span, a => { if (equivalenceKey == null || string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal)) { if (action == null) { action = a; } } }, CancellationToken.None); await RefactoringProvider.ComputeRefactoringsAsync(context).ConfigureAwait(false); Assert.True(action != null, "No code refactoring has been registered."); document = await document.ApplyCodeActionAsync(action).ConfigureAwait(false); semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); ImmutableArray <Diagnostic> newCompilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); VerifyCompilerDiagnostics(newCompilerDiagnostics); if (!Options.AllowNewCompilerDiagnostics) { VerifyNoNewCompilerDiagnostics(compilerDiagnostics, newCompilerDiagnostics); } string actual = await document.ToFullStringAsync(simplify : true, format : true).ConfigureAwait(false); Assert.Equal(expected, actual); }
internal async Task VerifyNoRefactoringAsync( string source, IEnumerable <TextSpan> spans, string equivalenceKey = null, CodeVerificationOptions options = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); options ??= Options; using (Workspace workspace = new AdhocWorkspace()) { Document document = WorkspaceFactory.CreateDocument(workspace.CurrentSolution, source, options); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken); ImmutableArray <Diagnostic> compilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); VerifyCompilerDiagnostics(compilerDiagnostics, options); using (IEnumerator <TextSpan> en = spans.GetEnumerator()) { if (!en.MoveNext()) { Assert.True(false, "Span on which a refactoring should be invoked was not found."); } do { cancellationToken.ThrowIfCancellationRequested(); var context = new CodeRefactoringContext( document, en.Current, a => { if (equivalenceKey == null || string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal)) { Assert.True(false, "No code refactoring expected."); } }, CancellationToken.None); await RefactoringProvider.ComputeRefactoringsAsync(context); } while (en.MoveNext()); } } }
public async Task Test(RefactoringProvider provider, string caseName, string actionTitle, string sourcePath, string resultPath) { var source = sourcePath.F(File.ReadAllText); var resultLines = resultPath.F(File.ReadAllLines); var result = string.Join("\r\n", resultLines.Skip(1)); var cursorPos = source.IndexOf(CursorSymbol); AreNotEqual(-1, cursorPos, "There are no cursor in test source"); AreEqual(-1, result.IndexOf(CursorSymbol), "There are cursor in result file"); source = source.Replace(CursorSymbol, ""); using (var workspace = GetWorkspace()) { var project = workspace.AddProject("TestProject", CSharp) .AddMetadataReference(corlibReference) .AddMetadataReference(s_systemCoreReference); var document = project.AddDocument("TestCode.cs", source); var cts = new CancellationTokenSource(); var actions = new List <CodeAction>(); var context = new CodeRefactoringContext( document: document, span: TextSpan.FromBounds(start: cursorPos, end: cursorPos), registerRefactoring: a => { actions.Add(a); }, cancellationToken: cts.Token); await provider.Provider.ComputeRefactoringsAsync(context); var action = actions.SingleOrDefault(r => r.Title.Trim() == actionTitle.Trim()); IsTrue(action != null, NotFoundMessage(actions)); await ApplyActions(workspace, action, cts); var newDocument = workspace.CurrentSolution.Projects.Single().Documents.Single(); var expectedTree = SyntaxFactory.ParseCompilationUnit(result).SyntaxTree; var actualTree = newDocument.GetSyntaxTreeAsync(cts.Token).Result; IsTrue( condition: SyntaxFactory.AreEquivalent(expectedTree, actualTree, false), message: BadResultMessage(actualTree)); } }
public bool IsValid(object caller, Condition condition) { if (WorkbenchSingleton.Workbench == null) { return(false); } ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider; if (provider == null) { return(false); } LanguageProperties language = ParserService.CurrentProjectContent.Language; if (language == null) { return(false); } if (string.IsNullOrEmpty(provider.TextEditor.FileName)) { return(false); } RefactoringProvider rp = language.RefactoringProvider; if (!rp.IsEnabledForFile(provider.TextEditor.FileName)) { return(false); } string supports = condition.Properties["supports"]; if (supports == "*") { return(true); } Type t = rp.GetType(); try { return((bool)t.InvokeMember("Supports" + supports, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty, null, rp, null)); } catch (Exception ex) { LoggingService.Warn(ex.ToString()); return(false); } }
protected override void Run(TextEditorControl textEditor, RefactoringProvider provider) { ResolveResult rr = ResolveAtCaret(textEditor); if (rr is MixedResolveResult) { rr = (rr as MixedResolveResult).PrimaryResult; } if (rr is TypeResolveResult) { IClass c = (rr as TypeResolveResult).ResolvedClass; if (c == null) { ShowUnknownSymbolError(); } else if (c.CompilationUnit.FileName == null) { ShowNoUserCodeError(); } else { FindReferencesAndRenameHelper.RenameClass(c); } } else if (rr is MemberResolveResult) { Rename((rr as MemberResolveResult).ResolvedMember); } else if (rr is MethodResolveResult) { Rename((rr as MethodResolveResult).GetMethodIfSingleOverload()); } else { ShowUnknownSymbolError(); } }
////private LanguageElement _activeElement; /// <summary> /// Initializes a new instance of the FormatXamlPlugin class. /// </summary> public FormatXamlAttributesPlugin() { _components = new System.ComponentModel.Container(); _refactorFormatXamlAttributes = new RefactoringProvider(_components); ((System.ComponentModel.ISupportInitialize)(_refactorFormatXamlAttributes)).BeginInit(); _refactorFormatXamlAttributes.ActionHintText = "Formats Attributes"; _refactorFormatXamlAttributes.DisplayName = "Format Xaml Attributes"; _refactorFormatXamlAttributes.AutoActivate = true; _refactorFormatXamlAttributes.AutoUndo = false; _refactorFormatXamlAttributes.Description = ""; _refactorFormatXamlAttributes.NeedsSelection = false; _refactorFormatXamlAttributes.ProviderName = "Format Xaml Attributes"; _refactorFormatXamlAttributes.Register = true; _refactorFormatXamlAttributes.SupportsAsyncMode = false; _refactorFormatXamlAttributes.LanguageSupported += _refactorFormatXamlAttributes_LanguageSupported; _refactorFormatXamlAttributes.CheckAvailability += _refactorFormatXaml_CheckAvailability; _refactorFormatXamlAttributes.PreparePreview += _refactorFormatXaml_PreparePreview; _refactorFormatXamlAttributes.Apply += _refactorFormatXaml_Apply; ((System.ComponentModel.ISupportInitialize)(_refactorFormatXamlAttributes)).EndInit(); }
protected abstract void Run(TextEditorControl textEditor, RefactoringProvider provider);
protected override void Run(ITextEditor textEditor, RefactoringProvider provider) { using (var pm = Gui.AsynchronousWaitDialog.ShowWaitDialog("${res:SharpDevelop.Refactoring.RemoveUnusedImports}")) { NamespaceRefactoringService.ManageUsings(pm, textEditor.FileName, textEditor.Document, true, true); } }
protected abstract void Run(ITextEditor textEditor, RefactoringProvider provider);
public async Task VerifyRefactoringAsync( string source, string expected, TextSpan span, string[] additionalSources = null, string equivalenceKey = null, CodeVerificationOptions options = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); using (Workspace workspace = new AdhocWorkspace()) { Project project = WorkspaceFactory.AddProject(workspace.CurrentSolution, options); Document document = WorkspaceFactory.AddDocument(project, source, additionalSources ?? Array.Empty <string>()); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); ImmutableArray <Diagnostic> compilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); if (options == null) { options = Options; } VerifyCompilerDiagnostics(compilerDiagnostics, options); CodeAction action = null; var context = new CodeRefactoringContext( document, span, a => { if (equivalenceKey == null || string.Equals(a.EquivalenceKey, equivalenceKey, StringComparison.Ordinal)) { if (action == null) { action = a; } } }, CancellationToken.None); await RefactoringProvider.ComputeRefactoringsAsync(context).ConfigureAwait(false); Assert.True(action != null, "No code refactoring has been registered."); document = await document.ApplyCodeActionAsync(action).ConfigureAwait(false); semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); ImmutableArray <Diagnostic> newCompilerDiagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken); VerifyCompilerDiagnostics(newCompilerDiagnostics, options); if (!options.AllowNewCompilerDiagnostics) { VerifyNoNewCompilerDiagnostics(compilerDiagnostics, newCompilerDiagnostics, options); } string actual = await document.ToFullStringAsync(simplify : true, format : true).ConfigureAwait(false); Assert.Equal(expected, actual); } }
protected override void Run(TextEditorControl textEditor, RefactoringProvider provider) { NamespaceRefactoringService.ManageUsings(textEditor.FileName, textEditor.Document, true, true); }