public static OmniSharpRefactoringContext GetContext(BufferParser bufferParser, Request request) { var q = bufferParser.ParsedContent(request.Buffer, request.FileName); var resolver = new CSharpAstResolver(q.Compilation, q.SyntaxTree, q.UnresolvedFile); var doc = new StringBuilderDocument(request.Buffer); var location = new TextLocation(request.Line, request.Column); OmniSharpRefactoringContext refactoringContext; if(request is CodeActionRequest) { var car = request as CodeActionRequest; if(car.SelectionStartColumn.HasValue) { var startLocation = new TextLocation(car.SelectionStartLine.Value, car.SelectionStartColumn.Value); var endLocation = new TextLocation(car.SelectionEndLine.Value, car.SelectionEndColumn.Value); refactoringContext = new OmniSharpRefactoringContext(doc, resolver, location, startLocation, endLocation); } else { refactoringContext = new OmniSharpRefactoringContext(doc, resolver, location); } } else { refactoringContext = new OmniSharpRefactoringContext(doc, resolver, location); } refactoringContext.Services.AddService (typeof(NamingConventionService), new DefaultNameService ()); return refactoringContext; }
public static OmniSharpRefactoringContext GetContext(BufferParser bufferParser, Request request) { var q = bufferParser.ParsedContent(request.Buffer, request.FileName); var resolver = new CSharpAstResolver(q.Compilation, q.SyntaxTree, q.UnresolvedFile); var doc = new StringBuilderDocument(request.Buffer); var location = new TextLocation(request.Line, request.Column); OmniSharpRefactoringContext refactoringContext; if (request is CodeActionRequest) { var car = request as CodeActionRequest; if (car.SelectionStartColumn.HasValue) { var startLocation = new TextLocation(car.SelectionStartLine.Value, car.SelectionStartColumn.Value); var endLocation = new TextLocation(car.SelectionEndLine.Value, car.SelectionEndColumn.Value); refactoringContext = new OmniSharpRefactoringContext(doc, resolver, location, startLocation, endLocation); } else { refactoringContext = new OmniSharpRefactoringContext(doc, resolver, location); } } else { refactoringContext = new OmniSharpRefactoringContext(doc, resolver, location); } refactoringContext.Services.AddService(typeof(NamingConventionService), new DefaultNameService()); refactoringContext.Services.AddService(typeof(CodeGenerationService), new DefaultCodeGenerationService()); return(refactoringContext); }
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 static OmniSharpRefactoringContext GetContext(BufferParser bufferParser, Request request) { var q = bufferParser.ParsedContent(request.Buffer, request.FileName); var resolver = new CSharpAstResolver(q.Compilation, q.SyntaxTree, q.UnresolvedFile); var doc = new StringBuilderDocument(request.Buffer); var location = new TextLocation(request.Line, request.Column); var refactoringContext = new OmniSharpRefactoringContext(doc, resolver, location); return refactoringContext; }
public static OmniSharpRefactoringContext GetContext(BufferParser bufferParser, Request request) { var q = bufferParser.ParsedContent(request.Buffer, request.FileName); var resolver = new CSharpAstResolver(q.Compilation, q.SyntaxTree, q.UnresolvedFile); var doc = new StringBuilderDocument(request.Buffer); var location = new TextLocation(request.Line, request.Column); var refactoringContext = new OmniSharpRefactoringContext(doc, resolver, location); return(refactoringContext); }
/// <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; }
public OmniSharpScript(OmniSharpRefactoringContext context, OmniSharpConfiguration config) : base(context.Document, config.CSharpFormattingOptions, config.TextEditorOptions) { _context = context; }
public OmniSharpScript(OmniSharpRefactoringContext context) : base(context.Document, FormattingOptionsFactory.CreateAllman(), new TextEditorOptions()) { _context = context; }
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; }