Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        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 SyntaxTree ParseStub(string continuation, bool appendSemicolon = true, string afterContinuation = null)
        {
            var mt = GetMemberTextToCaret();

            if (mt == null)
            {
                return(null);
            }

            string memberText      = mt.Item1;
            var    memberLocation  = mt.Item2;
            int    closingBrackets = 0;
            int    generatedLines  = 0;
            var    wrapper         = new StringBuilder();
            bool   wrapInClass     = memberLocation != new TextLocation(1, 1);

            if (wrapInClass)
            {
                wrapper.Append("package { class Stub {");
                wrapper.AppendLine();
                closingBrackets += 2;
                generatedLines++;
            }
            wrapper.Append(memberText);
            wrapper.Append(continuation);
            AppendMissingClosingBrackets(wrapper, memberText, appendSemicolon);
            wrapper.Append(afterContinuation);
            if (closingBrackets > 0)
            {
                wrapper.Append(new string('}', closingBrackets));
            }
            var parser = new PlayScriptParser();

            foreach (var sym in CompletionContextProvider.ConditionalSymbols)
            {
                parser.CompilerSettings.ConditionalSymbols.Add(sym);
            }
            parser.InitialLocation = new TextLocation(memberLocation.Line - generatedLines, 1);
            var result = parser.Parse(wrapper.ToString());

            return(result);
        }
		ExpressionResult GetNewExpressionAt(int offset)
		{
			var parser = new PlayScriptParser();
			string text = this.document.GetText(0, this.offset); 
			var sb = new StringBuilder(text);
			sb.Append("a ();");
			AppendMissingClosingBrackets(sb, text, false);
			
			var completionUnit = parser.Parse(sb.ToString());
			var loc = document.GetLocation(offset);
			
			var expr = completionUnit.GetNodeAt(loc, n => n is Expression);
			if (expr == null) {
				// try without ";"
				sb = new StringBuilder(text);
				sb.Append("a ()");
				AppendMissingClosingBrackets(sb, text, false);
				completionUnit = parser.Parse(sb.ToString());
				loc = document.GetLocation(offset);
				
				expr = completionUnit.GetNodeAt(loc, n => n is Expression);
				if (expr == null) {
					return null;
				}
			}
			return new ExpressionResult(expr, completionUnit);
		}
		ExpressionResult GetExpressionAt(int offset)
		{
			var parser = new PlayScriptParser();
			string text = this.document.GetText(0, this.offset); 
			var sb = new StringBuilder(text);
			sb.Append("a;");
			AppendMissingClosingBrackets(sb, text, false);
			var completionUnit = parser.Parse(sb.ToString());
			var loc = document.GetLocation(offset);
			
			var expr = completionUnit.GetNodeAt(
				loc,
				n => n is Expression || n is VariableDeclarationStatement
				);
			if (expr == null) {
				return null;
			}
			return new ExpressionResult(expr, completionUnit);
		}
Beispiel #6
0
        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 ICSharpCode.NRefactory.PlayScript.CSharpFormatter(
                policy.CreateOptions(),
                textEditorOptions
                )
            {
                FormattingMode = FormattingMode.Intrusive
            };

            var changes = formattingVisitor.AnalyzeFormatting(data.Document, compilationUnit);

            try {
                changes.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);
        }
		protected SyntaxTree ParseStub(string continuation, bool appendSemicolon = true, string afterContinuation = null)
		{
			var mt = GetMemberTextToCaret();
			if (mt == null) {
				return null;
			}

			string memberText = mt.Item1;
			var memberLocation = mt.Item2;
			int closingBrackets = 0;
			int generatedLines = 0;
			var wrapper = new StringBuilder();
			bool wrapInClass = memberLocation != new TextLocation(1, 1);
			if (wrapInClass) {
				wrapper.Append("package { class Stub {");
				wrapper.AppendLine();
				closingBrackets += 2;
				generatedLines++;
			}
			wrapper.Append(memberText);
			wrapper.Append(continuation);
			AppendMissingClosingBrackets(wrapper, memberText, appendSemicolon);
			wrapper.Append(afterContinuation);
			if (closingBrackets > 0) { 
				wrapper.Append(new string('}', closingBrackets));
			}
			var parser = new PlayScriptParser ();
			foreach (var sym in CompletionContextProvider.ConditionalSymbols)
				parser.CompilerSettings.ConditionalSymbols.Add (sym);
			parser.InitialLocation = new TextLocation(memberLocation.Line - generatedLines, 1);
			var result = parser.Parse(wrapper.ToString ());
			return result;
		}
        public static SyntaxTree Parse(ITextSource textSource, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var parser = new PlayScriptParser(settings);

            return(parser.Parse(textSource, fileName));
        }