protected override void Run () { MonoDevelop.Ide.Gui.Document doc = IdeApp.Workbench.ActiveDocument; PlayScriptParser parser = new PlayScriptParser (); var unit = parser.Parse (doc.Editor); if (unit == null) return; var node = unit.GetNodeAt (doc.Editor.Caret.Line, doc.Editor.Caret.Column); if (node == null) return; Stack<AstNode > nodeStack = new Stack<AstNode> (); nodeStack.Push (node); if (doc.Editor.IsSomethingSelected) { while (node != null && doc.Editor.MainSelection.IsSelected (node.StartLocation, node.EndLocation)) { node = node.Parent; if (node != null) { if (nodeStack.Count > 0 && nodeStack.Peek ().StartLocation == node.StartLocation && nodeStack.Peek ().EndLocation == node.EndLocation) nodeStack.Pop (); nodeStack.Push (node); } } } if (nodeStack.Count > 2) { nodeStack.Pop (); // parent nodeStack.Pop (); // current node node = nodeStack.Pop (); // next children in which the caret is doc.Editor.SetSelection (node.StartLocation, node.EndLocation); } else { doc.Editor.ClearSelection (); } }
protected override void Run () { MonoDevelop.Ide.Gui.Document doc = IdeApp.Workbench.ActiveDocument; PlayScriptParser parser = new PlayScriptParser (); var unit = parser.Parse (doc.Editor); if (unit == null) return; var node = unit.GetNodeAt (doc.Editor.Caret.Location); if (node == null) return; if (doc.Editor.IsSomethingSelected) { while (node != null && doc.Editor.MainSelection.IsSelected (node.StartLocation, node.EndLocation)) { node = node.Parent; } } if (node != null) doc.Editor.SetSelection (node.StartLocation, node.EndLocation); }
public override IEnumerable<MemberReference> FindReferences (Project project, IProjectContent content, IEnumerable<FilePath> possibleFiles, IEnumerable<object> members) { if (content == null) throw new ArgumentNullException ("content", "Project content not set."); SetPossibleFiles (possibleFiles); SetSearchedMembers (members); var scopes = searchedMembers.Select (e => refFinder.GetSearchScopes (e as IEntity)); var compilation = project != null ? TypeSystemService.GetCompilation (project) : content.CreateCompilation (); List<MemberReference> refs = new List<MemberReference> (); foreach (var opendoc in openDocuments) { foreach (var newRef in FindInDocument (opendoc.Item2)) { if (newRef == null || refs.Any (r => r.FileName == newRef.FileName && r.Region == newRef.Region)) continue; refs.Add (newRef); } } foreach (var file in files) { string text = Mono.TextEditor.Utils.TextFileUtility.ReadAllText (file); if (memberName != null && text.IndexOf (memberName, StringComparison.Ordinal) < 0 && (keywordName == null || text.IndexOf (keywordName, StringComparison.Ordinal) < 0)) continue; using (var editor = TextEditorData.CreateImmutable (text)) { editor.Document.FileName = file; var unit = new PlayScriptParser ().Parse (editor); if (unit == null) continue; var storedFile = content.GetFile (file); var parsedFile = storedFile as CSharpUnresolvedFile; if (parsedFile == null && storedFile is ParsedDocumentDecorator) { parsedFile = ((ParsedDocumentDecorator)storedFile).ParsedFile as CSharpUnresolvedFile; } if (parsedFile == null) { // for fallback purposes - should never happen. parsedFile = unit.ToTypeSystem (); content = content.AddOrUpdateFiles (parsedFile); compilation = content.CreateCompilation (); } foreach (var scope in scopes) { refFinder.FindReferencesInFile ( scope, parsedFile, unit, compilation, (astNode, result) => { var newRef = GetReference (result, astNode, file, editor); if (newRef == null || refs.Any (r => r.FileName == newRef.FileName && r.Region == newRef.Region)) return; refs.Add (newRef); }, CancellationToken.None ); } } } return refs; }
public string FormatText (PlayScriptFormattingPolicy policy, TextStylePolicy textPolicy, string mimeType, string input, int startOffset, int endOffset) { var data = new TextEditorData (); data.Document.SuppressHighlightUpdate = true; data.Document.MimeType = mimeType; data.Document.FileName = "toformat.cs"; if (textPolicy != null) { data.Options.TabsToSpaces = textPolicy.TabsToSpaces; data.Options.TabSize = textPolicy.TabWidth; data.Options.IndentationSize = textPolicy.IndentWidth; data.Options.IndentStyle = textPolicy.RemoveTrailingWhitespace ? IndentStyle.Virtual : IndentStyle.Smart; } data.Text = input; // System.Console.WriteLine ("-----"); // System.Console.WriteLine (data.Text.Replace (" ", ".").Replace ("\t", "->")); // System.Console.WriteLine ("-----"); var parser = new PlayScriptParser (); var compilationUnit = parser.Parse (data); bool hadErrors = parser.HasErrors; if (hadErrors) { // foreach (var e in parser.ErrorReportPrinter.Errors) // Console.WriteLine (e.Message); return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset)); } var originalVersion = data.Document.Version; var textEditorOptions = data.CreateNRefactoryTextEditorOptions (); var formattingVisitor = new AstFormattingVisitor ( policy.CreateOptions (), data.Document, textEditorOptions ) { HadErrors = hadErrors, FormattingMode = FormattingMode.Intrusive }; compilationUnit.AcceptVisitor (formattingVisitor); try { formattingVisitor.ApplyChanges (startOffset, endOffset - startOffset); } catch (Exception e) { LoggingService.LogError ("Error in code formatter", e); return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset)); } // check if the formatter has produced errors parser = new PlayScriptParser (); parser.Parse (data); if (parser.HasErrors) { LoggingService.LogError ("C# formatter produced source code errors. See console for output."); return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset)); } var currentVersion = data.Document.Version; string result = data.GetTextBetween (startOffset, originalVersion.MoveOffsetTo (currentVersion, endOffset, ICSharpCode.NRefactory.Editor.AnchorMovementType.Default)); data.Dispose (); return result; }