public RenameResponse Rename(RenameRequest req) { var project = _solution.ProjectContainingFile(req.FileName); var syntaxTree = project.CreateParser().Parse(req.Buffer, req.FileName); var sourceNode = syntaxTree.GetNodeAt(req.Line, req.Column); if(sourceNode == null) return new RenameResponse(); var originalName = sourceNode.GetText(); IEnumerable<AstNode> nodes = _findUsagesHandler.FindUsageNodes(req).ToArray(); var response = new RenameResponse(); var modfiedFiles = new List<ModifiedFileResponse>(); response.Changes = modfiedFiles; foreach (IGrouping<string, AstNode> groupedNodes in nodes.GroupBy(n => n.GetRegion().FileName.FixPath())) { string fileName = groupedNodes.Key; OmniSharpRefactoringContext context; if (groupedNodes.Key != req.FileName) { var file = _solution.GetFile(fileName); var bufferParser = new BufferParser(_solution); var content = bufferParser.ParsedContent(file.Document.Text, file.FileName); var resolver = new CSharpAstResolver(content.Compilation, content.SyntaxTree, content.UnresolvedFile); context = new OmniSharpRefactoringContext(file.Document, resolver); } else { context = OmniSharpRefactoringContext.GetContext(_bufferParser, req); } string modifiedBuffer = null; foreach (var node in groupedNodes.Where(n => n.GetText() == originalName)) { using (var script = new OmniSharpScript(context)) { script.Rename(node, req.RenameTo); modifiedBuffer = script.CurrentDocument.Text; } } if (modifiedBuffer != null) { var modifiedFile = new ModifiedFileResponse { FileName = fileName, Buffer = modifiedBuffer }; modfiedFiles.Add(modifiedFile); response.Changes = modfiedFiles; _bufferParser.ParsedContent(modifiedBuffer, fileName); _solution.GetFile(fileName).Update(modifiedBuffer); } } return response; }
public FixUsingsResponse FixUsings(Request request) { _fileName = request.FileName; string buffer = RemoveUsings(request.Buffer); buffer = SortUsings(buffer); buffer = AddLinqForQueryIfMissing(buffer); bool ambiguousResultsFound = false; bool usingsAdded = true; while (usingsAdded) { var content = _bufferParser.ParsedContent(buffer, _fileName); var tree = content.SyntaxTree; var resolver = new CSharpAstResolver(content.Compilation, content.SyntaxTree, content.UnresolvedFile); var unresolvedNodes = GetAllUnresolvedNodes(tree, resolver).Select(nr => GetNodeToAddUsing(nr)); usingsAdded = false; request.Buffer = buffer; var outerContext = OmniSharpRefactoringContext.GetContext(_bufferParser, request); using (var script = new OmniSharpScript(outerContext, _config)) { foreach (var unresolvedNode in unresolvedNodes) { _logger.Info(unresolvedNode); var requestForNode = CreateRequest(buffer, unresolvedNode); var innerContext = OmniSharpRefactoringContext.GetContext(_bufferParser, requestForNode); var addUsingAction = new AddUsingAction(); var actions = addUsingAction.GetActions(innerContext).Where(a => a.Description.StartsWith("using")).ToArray(); if (actions.Length == 1) { var a = actions[0]; _logger.Info("Adding " + a.Description); a.Run(script); usingsAdded = true; break; } ambiguousResultsFound |= actions.Length > 1; } } buffer = outerContext.Document.Text; } IEnumerable<QuickFix> ambiguous = Enumerable.Empty<QuickFix>(); if (ambiguousResultsFound) { ambiguous = GetAmbiguousNodes(buffer, request.FileName); } return new FixUsingsResponse(buffer, ambiguous); }
public RunCodeActionsResponse RunCodeAction(CodeActionRequest req) { var actions = GetContextualCodeActions(req).ToList(); if(req.CodeAction > actions.Count) return new RunCodeActionsResponse(); var context = OmniSharpRefactoringContext.GetContext(_bufferParser, req); using (var script = new OmniSharpScript(context, _config)) { CodeAction action = actions[req.CodeAction]; action.Run(script); } return new RunCodeActionsResponse {Text = context.Document.Text}; }
public RunCodeIssuesResponse FixCodeIssue(Request req) { var issues = GetContextualCodeActions(req).ToList(); var issue = issues.FirstOrDefault(i => i.Start.Line == req.Line); if (issue == null) return new RunCodeIssuesResponse { Text = req.Buffer }; var context = OmniSharpRefactoringContext.GetContext(_bufferParser, req); using (var script = new OmniSharpScript(context, _config)) { var action = issue.Actions.FirstOrDefault(); if (action != null) { action.Run(script); return new RunCodeIssuesResponse {Text = context.Document.Text}; } } return new RunCodeIssuesResponse {Text = req.Buffer}; }
/// <summary> /// Inserts the given EntityDeclaration with the given /// script at the end of the type declaration under the /// cursor (e.g. class / struct). /// </summary> /// <remarks> /// Alters the given script. Returns its CurrentDocument /// property. Alters the given memberDeclaration, adding /// Modifiers.Override to its Modifiers as well as removing /// Modifiers.Virtual. /// </remarks> IDocument runOverrideTargetWorker ( Request request , OmniSharpRefactoringContext refactoringContext , ParsedResult parsedContent , EntityDeclaration memberDeclaration , OmniSharpScript script) { // Add override flag memberDeclaration.Modifiers |= Modifiers.Override; // Remove virtual flag memberDeclaration.Modifiers &= ~ Modifiers.Virtual; // The current type declaration, e.g. class, struct.. var typeDeclaration = parsedContent.SyntaxTree.GetNodeAt ( refactoringContext.Location , n => n.NodeType == NodeType.TypeDeclaration); // Even empty classes have nodes, so this works var lastNode = typeDeclaration.Children.Last(); script.InsertBefore ( node : lastNode , newNode : memberDeclaration); script.FormatText(memberDeclaration); return script.CurrentDocument; }
string AddUsingLinq(AstNode astNode, string buffer) { var request = CreateRequest(buffer, astNode); var context = OmniSharpRefactoringContext.GetContext(_bufferParser, request); var script = new OmniSharpScript(context, _config); UsingHelper.InsertUsingAndRemoveRedundantNamespaceUsage(context, script, "System.Linq"); return context.Document.Text; }
string RunActions(OmniSharpRefactoringContext context, IEnumerable<CodeAction> actions) { using (var script = new OmniSharpScript(context, _config)) { foreach (var action in actions) { if (action != null) { action.Run(script); } } } return context.Document.Text; }