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);
		}
Ejemplo n.º 3
0
 public static ICSharpCode.NRefactory.PlayScript.SyntaxTree Parse(this ICSharpCode.NRefactory.PlayScript.PlayScriptParser parser, TextEditorData data)
 {
     using (var stream = data.OpenStream()) {
         return(parser.Parse(stream, data.Document.FileName));
     }
 }
        public override ParsedDocument Parse(bool storeAst, string fileName, System.IO.TextReader content, MonoDevelop.Projects.Project project = null)
        {
            var parser = new ICSharpCode.NRefactory.PlayScript.PlayScriptParser(GetCompilerArguments(project));

            parser.GenerateTypeSystemMode = !storeAst;
            var result = new ParsedDocumentDecorator();

            if (project != null)
            {
                var projectFile = project.Files.GetFile(fileName);
                if (projectFile != null && projectFile.BuildAction != BuildAction.Compile)
                {
                    result.Flags |= ParsedDocumentFlags.NonSerializable;
                }
            }

            var tagComments = CommentTag.SpecialCommentTags.Select(t => t.Tag).ToArray();

            parser.CompilationUnitCallback = delegate(CompilerCompilationUnit top) {
                foreach (var special in top.SpecialsBag.Specials)
                {
                    var comment = special as SpecialsBag.Comment;
                    if (comment != null)
                    {
                        VisitComment(result, comment, tagComments);
                    }
                    else
                    {
                        if (storeAst)
                        {
                            var ppd = special as SpecialsBag.PreProcessorDirective;
                            if (ppd != null)
                            {
                                VisitPreprocessorDirective(result, ppd);
                            }
                        }
                    }
                }
            };

            var unit = parser.Parse(content, fileName);

            unit.Freeze();
            var pf = unit.ToTypeSystem();

            try {
                pf.LastWriteTime = System.IO.File.GetLastWriteTimeUtc(fileName);
            } catch (Exception) {
                pf.LastWriteTime = DateTime.UtcNow;
            }

            result.LastWriteTimeUtc = pf.LastWriteTime.Value;
            result.ParsedFile       = pf;
            result.Add(GetSemanticTags(unit));

            result.CreateRefactoringContext           = (doc, token) => new MDRefactoringContext(doc, doc.Editor.Caret.Location, token);
            result.CreateRefactoringContextWithEditor = (data, resolver, token) => new MDRefactoringContext((DotNetProject)project, data, result, (CSharpAstResolver)resolver, TextLocation.Empty, token);

            if (storeAst)
            {
                result.Ast = unit;
                result.Add(GenerateFoldings(unit, result));
            }
            return(result);
        }
		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;
		}