示例#1
1
		public override void CorrectIndenting (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, 
			TextEditorData data, int line)
		{
			LineSegment lineSegment = data.Document.GetLine (line);
			if (lineSegment == null)
				return;

			var policy = policyParent.Get<CFormattingPolicy> (mimeTypeChain);
			var tracker = new DocumentStateTracker<CIndentEngine> (new CIndentEngine (policy), data);
			tracker.UpdateEngine (lineSegment.Offset);
			for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.EditableLength; i++) {
				tracker.Engine.Push (data.Document.GetCharAt (i));
			}

			string curIndent = lineSegment.GetIndentation (data.Document);

			int nlwsp = curIndent.Length;
			if (!tracker.Engine.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.Length && data.Document.GetCharAt (lineSegment.Offset + nlwsp) == '*')) {
				// Possibly replace the indent
				string newIndent = tracker.Engine.ThisLineIndent;
				if (newIndent != curIndent) 
					data.Replace (lineSegment.Offset, nlwsp, newIndent);
			}
			tracker.Dispose ();
		}
        public override void CorrectIndenting(PolicyContainer policyParent, IEnumerable <string> mimeTypeChain,
                                              TextEditorData data, int line)
        {
            DocumentLine lineSegment = data.Document.GetLine(line);

            if (lineSegment == null)
            {
                return;
            }

            var policy     = policyParent.Get <CSharpFormattingPolicy> (mimeTypeChain);
            var textPolicy = policyParent.Get <TextStylePolicy> (mimeTypeChain);
            var tracker    = new DocumentStateTracker <CSharpIndentEngine> (new CSharpIndentEngine(policy, textPolicy), data);

            tracker.UpdateEngine(lineSegment.Offset);
            for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.Length; i++)
            {
                tracker.Engine.Push(data.Document.GetCharAt(i));
            }

            string curIndent = lineSegment.GetIndentation(data.Document);

            int nlwsp = curIndent.Length;

            if (!tracker.Engine.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.LengthIncludingDelimiter && data.Document.GetCharAt(lineSegment.Offset + nlwsp) == '*'))
            {
                // Possibly replace the indent
                string newIndent = tracker.Engine.ThisLineIndent;
                if (newIndent != curIndent)
                {
                    data.Replace(lineSegment.Offset, nlwsp, newIndent);
                }
            }
            tracker.Dispose();
        }
示例#3
0
		public override void CorrectIndenting (object textEditorData, int line)
		{
			TextEditorData data = (TextEditorData)textEditorData;
			LineSegment lineSegment = data.Document.GetLine (line);
			if (lineSegment == null)
				return;
			IEnumerable<string> types = MonoDevelop.Ide.DesktopService.GetMimeTypeInheritanceChain (CSharpFormatter.MimeType);
			var policy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<CSharpFormattingPolicy> (types);
			DocumentStateTracker<CSharpIndentEngine> tracker = new DocumentStateTracker<CSharpIndentEngine> (new CSharpIndentEngine (policy), data);
			tracker.UpdateEngine (lineSegment.Offset);
			for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.EditableLength; i++) {
				tracker.Engine.Push (data.Document.GetCharAt (i));
			}
			
			string curIndent = lineSegment.GetIndentation (data.Document);
			
			int nlwsp = curIndent.Length;
			if (!tracker.Engine.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.Length && data.Document.GetCharAt (lineSegment.Offset + nlwsp) == '*')) {
				// Possibly replace the indent
				string newIndent = tracker.Engine.ThisLineIndent;
				if (newIndent != curIndent) 
					data.Replace (lineSegment.Offset, nlwsp, newIndent);
			}
			tracker.Dispose ();
		}
示例#4
0
        protected override void Initialize()
        {
            base.Initialize();

            // Delay the execution of UpdateOwnerProjects since it may end calling Document.AttachToProject,
            // which shouldn't be called while the extension chain is being initialized.
            // TODO: Move handling of owner projects to Document
            Application.Invoke((o, args) => {
                UpdateOwnerProjects();
            });

            var parser = new XmlParser(CreateRootState(), false);

            tracker = new DocumentStateTracker <XmlParser> (parser, Editor);
            DocumentContext.DocumentParsed += UpdateParsedDocument;
            Editor.CaretPositionChanged    += HandleCaretPositionChanged;

            if (DocumentContext.ParsedDocument != null)
            {
                lastCU = DocumentContext.ParsedDocument;
                OnParsedDocumentUpdated();
            }

            if (IdeApp.Workspace != null)
            {
                IdeApp.Workspace.FileAddedToProject     += HandleProjectChanged;
                IdeApp.Workspace.FileRemovedFromProject += HandleProjectChanged;
            }
        }
		DocumentStateTracker<CSharpIndentEngine> CreateTracker (TextEditorData data)
		{
			var policy = PolicyService.InvariantPolicies.Get <CSharpFormattingPolicy> ("text/x-csharp");
			var textStylePolicy = PolicyService.InvariantPolicies.Get <TextStylePolicy> ("text/x-csharp");
			var result = new DocumentStateTracker<CSharpIndentEngine> (new CSharpIndentEngine (policy, textStylePolicy), data);
			result.UpdateEngine ();
			return result;
		}
 public override void Dispose()
 {
     if (tracker != null)
     {
         tracker = null;
         base.Dispose();
     }
 }
		public override void Dispose ()
		{
			if (tracker != null) {
				tracker = null;
				MonoDevelop.Projects.Dom.Parser.ProjectDomService.ParsedDocumentUpdated
					-= OnParseInformationChanged;
				base.Dispose ();
			}
		}
        DocumentStateTracker <CSharpIndentEngine> CreateTracker(TextEditorData data)
        {
            var policy          = PolicyService.InvariantPolicies.Get <CSharpFormattingPolicy> ("text/x-csharp");
            var textStylePolicy = PolicyService.InvariantPolicies.Get <TextStylePolicy> ("text/x-csharp");
            var result          = new DocumentStateTracker <CSharpIndentEngine> (new CSharpIndentEngine(policy, textStylePolicy), data);

            result.UpdateEngine();
            return(result);
        }
 public override void Dispose()
 {
     if (tracker != null)
     {
         tracker = null;
         MonoDevelop.Projects.Dom.Parser.ProjectDomService.ParsedDocumentUpdated
             -= OnParseInformationChanged;
         base.Dispose();
     }
 }
		public override void Initialize ()
		{
			base.Initialize ();
			Parser parser = new Parser (CreateRootState (), false);
			tracker = new DocumentStateTracker<Parser> (parser, Editor);
			MonoDevelop.Projects.Dom.Parser.ProjectDomService.ParsedDocumentUpdated += OnParseInformationChanged;
			
			if (Document.ParsedDocument != null) {
				lastCU = Document.ParsedDocument;
				OnParsedDocumentUpdated ();
			}
		}
示例#11
0
 public override void Dispose()
 {
     if (tracker != null)
     {
         tracker = null;
         base.Dispose();
     }
     if (IdeApp.Workspace != null)
     {
         IdeApp.Workspace.FileAddedToProject     -= HandleProjectChanged;
         IdeApp.Workspace.FileRemovedFromProject -= HandleProjectChanged;
     }
 }
        public override void Initialize()
        {
            base.Initialize();
            Parser parser = new Parser(CreateRootState(), false);

            tracker = new DocumentStateTracker <Parser> (parser, Editor);
            MonoDevelop.Projects.Dom.Parser.ProjectDomService.ParsedDocumentUpdated += OnParseInformationChanged;

            if (Document.ParsedDocument != null)
            {
                lastCU = Document.ParsedDocument;
                OnParsedDocumentUpdated();
            }
        }
		public void StateShouldBeRazorRootStateAfterCodeBlock ()
		{
			editor.Text = 
@"@{
}

";
			var parser = new XmlParser (new RazorRootState (), false);
			var tracker = new DocumentStateTracker<XmlParser> (parser, editor);
			editor.CaretLine = 3;
			tracker.UpdateEngine ();

			Assert.IsInstanceOf<RazorRootState> (tracker.Engine.CurrentState);
		}
示例#14
0
        void InitTracker()
        {
            //if there's a CSharpTextEditorCompletion in the extension chain, we can reuse its stateTracker
            CSharpTextEditorCompletion c = this.Document.GetContent <CSharpTextEditorCompletion> ();

            if (c != null && c.StateTracker != null)
            {
                stateTracker = c.StateTracker;
            }
            else
            {
                stateTracker = new DocumentStateTracker <CSharpIndentEngine> (new CSharpIndentEngine(policy), textEditorData);
            }
        }
        public void StateShouldBeRazorRootStateAfterCodeBlock()
        {
            editor.Text =
                @"@{
}

";
            var parser  = new XmlParser(new RazorRootState(), false);
            var tracker = new DocumentStateTracker <XmlParser> (parser, editor);

            editor.CaretLine = 3;
            tracker.UpdateEngine();

            Assert.IsInstanceOf <RazorRootState> (tracker.Engine.CurrentState);
        }
		public override void Initialize ()
		{
			base.Initialize ();
			Parser parser = new Parser (CreateRootState (), false);
			tracker = new DocumentStateTracker<Parser> (parser, Editor);
			Document.DocumentParsed += delegate {
				lastCU = Document.ParsedDocument;
				OnParsedDocumentUpdated ();
			};
			
			if (Document.ParsedDocument != null) {
				lastCU = Document.ParsedDocument;
				OnParsedDocumentUpdated ();
			}
		}
示例#17
0
        public override void Dispose()
        {
            if (tracker != null)
            {
                tracker = null;
                base.Dispose();
            }

            Document.DocumentParsed -= UpdateParsedDocument;

            if (IdeApp.Workspace != null)
            {
                IdeApp.Workspace.FileAddedToProject     -= HandleProjectChanged;
                IdeApp.Workspace.FileRemovedFromProject -= HandleProjectChanged;
            }
        }
        public override void Initialize()
        {
            base.Initialize();
            Parser parser = new Parser(CreateRootState(), false);

            tracker = new DocumentStateTracker <Parser> (parser, Editor);
            Document.DocumentParsed += delegate {
                lastCU = Document.ParsedDocument;
                OnParsedDocumentUpdated();
            };

            if (Document.ParsedDocument != null)
            {
                lastCU = Document.ParsedDocument;
                OnParsedDocumentUpdated();
            }
        }
        public override void CorrectIndenting(object textEditorData, int line)
        {
            TextEditorData data        = (TextEditorData)textEditorData;
            LineSegment    lineSegment = data.Document.GetLine(line);

            if (lineSegment == null)
            {
                return;
            }
            IEnumerable <string> types = MonoDevelop.Ide.DesktopService.GetMimeTypeInheritanceChain(CSharpFormatter.MimeType);
            var policy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <CSharpFormattingPolicy> (types);
            DocumentStateTracker <CSharpIndentEngine> tracker = new DocumentStateTracker <CSharpIndentEngine> (new CSharpIndentEngine(policy), data);

            tracker.UpdateEngine(lineSegment.Offset);
            for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.EditableLength; i++)
            {
                tracker.Engine.Push(data.Document.GetCharAt(i));
            }

            string curIndent = lineSegment.GetIndentation(data.Document);

            int nlwsp = curIndent.Length;

//			int cursor = data.Caret.Offset;
//			int pos = lineSegment.Offset;
//			int offset = cursor > pos + nlwsp ? cursor - (pos + nlwsp) : 0;
            if (!tracker.Engine.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.Length && data.Document.GetCharAt(lineSegment.Offset + nlwsp) == '*'))
            {
                // Possibly replace the indent
                string newIndent = tracker.Engine.ThisLineIndent;
//				int newIndentLength = newIndent.Length;
                if (newIndent != curIndent)
                {
                    data.Replace(lineSegment.Offset, nlwsp, newIndent);
                }
            }

/*
 *                      string endIndent = tracker.Engine.ThisLineIndent;
 *                      string indent = endIndent.Length < beginIndent.Length ? endIndent : beginIndent;
 *                      if (indent != curIndent)
 *                              data.Replace (lineSegment.Offset, curIndent.Length, indent);
 */
            tracker.Dispose();
        }
示例#20
0
		public override void Dispose ()
		{
			Editor.CaretPositionChanged -= HandleCaretPositionChanged;

			if (tracker != null) {
				tracker.Dispose ();
				tracker = null;
			}

			DocumentContext.DocumentParsed -= UpdateParsedDocument;

			if (IdeApp.Workspace != null) {
				IdeApp.Workspace.FileAddedToProject -= HandleProjectChanged;
				IdeApp.Workspace.FileRemovedFromProject -= HandleProjectChanged;
			}

			base.Dispose ();
		}
		public override void Initialize ()
		{
			base.Initialize ();
			UpdateOwnerProjects ();
			Parser parser = new Parser (CreateRootState (), false);
			tracker = new DocumentStateTracker<Parser> (parser, Editor);
			Document.DocumentParsed += delegate {
				lastCU = Document.ParsedDocument;
				OnParsedDocumentUpdated ();
			};
			
			if (Document.ParsedDocument != null) {
				lastCU = Document.ParsedDocument;
				OnParsedDocumentUpdated ();
			}
			if (IdeApp.Workspace != null) {
				IdeApp.Workspace.FileAddedToProject += HandleProjectChanged;
				IdeApp.Workspace.FileRemovedFromProject += HandleProjectChanged;
			}
		}
示例#22
0
        public override void Initialize()
        {
            base.Initialize();
            UpdateOwnerProjects();
            Parser parser = new Parser(CreateRootState(), false);

            tracker = new DocumentStateTracker <Parser> (parser, Editor);
            Document.DocumentParsed += delegate {
                lastCU = Document.ParsedDocument;
                OnParsedDocumentUpdated();
            };

            if (Document.ParsedDocument != null)
            {
                lastCU = Document.ParsedDocument;
                OnParsedDocumentUpdated();
            }
            if (IdeApp.Workspace != null)
            {
                IdeApp.Workspace.FileAddedToProject     += HandleProjectChanged;
                IdeApp.Workspace.FileRemovedFromProject += HandleProjectChanged;
            }
        }
示例#23
0
        public override void Initialize()
        {
            base.Initialize();

            textEditorData = Document.Editor;
            if (textEditorData != null)
            {
                textEditorData.Options.Changed += delegate {
                    textEditorData.IndentationTracker = new DIndentationTracker(
                        textEditorData,
                        new DocumentStateTracker <DIndentEngine> (new DIndentEngine(Policy, TextStyle), textEditorData)
                        );
                };
                textEditorData.IndentationTracker = new DIndentationTracker(
                    textEditorData,
                    new DocumentStateTracker <DIndentEngine> (new DIndentEngine(Policy, TextStyle), textEditorData)
                    );
            }

            // Init tracker
            stateTracker = new DocumentStateTracker <DIndentEngine> (new DIndentEngine(Policy, TextStyle), textEditorData);

            Document.Editor.Paste += HandleTextPaste;
        }
 void InitTracker()
 {
     //if there's a FSharpTextEditorCompletion in the extension chain, we can reuse its stateTracker
     FSharpTextEditorCompletion c = this.Document.GetContent<FSharpTextEditorCompletion> ();
     if (c != null && c.StateTracker != null) {
         stateTracker = c.StateTracker;
     } else {
         stateTracker = new DocumentStateTracker<FSharpIndentEngine> (new FSharpIndentEngine (), Editor);
     }
 }
 void InitTracker()
 {
     stateTracker = new DocumentStateTracker<TypeScriptIndentEngine> (new TypeScriptIndentEngine (policy, textStylePolicy), Editor);
 }
示例#26
0
 public DIndentationTracker(Mono.TextEditor.TextEditorData data, DocumentStateTracker <DIndentEngine> stateTracker)
 {
     this.data         = data;
     this.stateTracker = stateTracker;
 }
		public override void Initialize ()
		{
			base.Initialize ();
			S.Parser parser = new S.Parser (new S.XmlFreeState (), true);
			tracker = new DocumentStateTracker<S.Parser> (parser, Editor);
		}
		public override void Dispose ()
		{
			Editor.CaretPositionChanged -= HandleCaretPositionChanged;

			if (tracker != null) {
				tracker.Dispose ();
				tracker = null;
			}

			DocumentContext.DocumentParsed -= UpdateParsedDocument;

			if (IdeApp.Workspace != null) {
				IdeApp.Workspace.FileAddedToProject -= HandleProjectChanged;
				IdeApp.Workspace.FileRemovedFromProject -= HandleProjectChanged;
			}

			base.Dispose ();
		}
		/*		void TextCut (object sender, ReplaceEventArgs e)
		{
			if (!string.IsNullOrEmpty (e.Value) || e.Count == 0)
				return;
			RunFormatterAt (e.Offset);
		}*/


		#region Sharing the tracker

		void InitTracker ()
		{
			stateTracker = new DocumentStateTracker<CSharpIndentEngine> (new CSharpIndentEngine (Policy, TextStylePolicy), textEditorData);
		}
		public override void Dispose ()
		{
			if (tracker != null) {
				tracker = null;
				base.Dispose ();
			}
			if (IdeApp.Workspace != null) {
				IdeApp.Workspace.FileAddedToProject -= HandleProjectChanged;
				IdeApp.Workspace.FileRemovedFromProject -= HandleProjectChanged;
			}
		}
		public override void Initialize ()
		{
			base.Initialize ();

			textEditorData = Document.Editor;
			if (textEditorData != null) {
				textEditorData.Options.Changed += delegate {
					textEditorData.IndentationTracker = new DIndentationTracker (
						textEditorData,
						new DocumentStateTracker<DIndentEngine> (new DIndentEngine (Policy, TextStyle), textEditorData)
					);
				};
				textEditorData.IndentationTracker = new DIndentationTracker (
					textEditorData,
					new DocumentStateTracker<DIndentEngine> (new DIndentEngine (Policy, TextStyle), textEditorData)
				);
			}

			// Init tracker
			stateTracker = new DocumentStateTracker<DIndentEngine> (new DIndentEngine (Policy, TextStyle), textEditorData);
			
			Document.Editor.Paste += HandleTextPaste;
		}
		void InitTracker ()
		{
			stateTracker = new DocumentStateTracker<CIndentEngine> (new CIndentEngine (policy), textEditorData);
		}
示例#33
0
 public override void Initialize()
 {
     base.Initialize();
     S.Parser parser = new S.Parser(new S.XmlFreeState(), true);
     tracker = new DocumentStateTracker <S.Parser> (parser, Editor);
 }
示例#34
0
 public DIndentationTracker(Mono.TextEditor.TextEditorData data, DocumentStateTracker<DIndentEngine> stateTracker)
 {
     this.data = data;
     this.stateTracker = stateTracker;
 }
		public override void Dispose ()
		{
			if (tracker != null) {
				tracker = null;
				base.Dispose ();
			}

			Document.DocumentParsed -= UpdateParsedDocument;

			if (IdeApp.Workspace != null) {
				IdeApp.Workspace.FileAddedToProject -= HandleProjectChanged;
				IdeApp.Workspace.FileRemovedFromProject -= HandleProjectChanged;
			}
		}
		public override void Dispose ()
		{
			if (tracker != null) {
				tracker = null;
				base.Dispose ();
			}
		}
        /*		void TextCut (object sender, ReplaceEventArgs e)
         * {
         *      if (!string.IsNullOrEmpty (e.Value) || e.Count == 0)
         *              return;
         *      RunFormatterAt (e.Offset);
         * }*/


        #region Sharing the tracker

        void InitTracker()
        {
            stateTracker = new DocumentStateTracker <CSharpIndentEngine> (new CSharpIndentEngine(Policy, TextStylePolicy), textEditorData);
        }
        public static bool FixLineStart(TextEditorData textEditorData, DocumentStateTracker <CSharpIndentEngine> stateTracker, int lineNumber)
        {
            if (lineNumber > DocumentLocation.MinLine)
            {
                DocumentLine line = textEditorData.Document.GetLine(lineNumber);
                if (line == null)
                {
                    return(false);
                }

                DocumentLine prevLine = textEditorData.Document.GetLine(lineNumber - 1);
                if (prevLine == null)
                {
                    return(false);
                }
                string trimmedPreviousLine = textEditorData.Document.GetTextAt(prevLine).TrimStart();

                //xml doc comments
                //check previous line was a doc comment
                //check there's a following line?
                if (trimmedPreviousLine.StartsWith("///", StringComparison.Ordinal))
                {
                    if (textEditorData.GetTextAt(line.Offset, line.Length).TrimStart().StartsWith("///", StringComparison.Ordinal))
                    {
                        return(false);
                    }
                    //check that the newline command actually inserted a newline
                    textEditorData.EnsureCaretIsNotVirtual();
                    string nextLine = textEditorData.Document.GetTextAt(textEditorData.Document.GetLine(lineNumber + 1)).TrimStart();

                    if (trimmedPreviousLine.Length > "///".Length || nextLine.StartsWith("///", StringComparison.Ordinal))
                    {
                        var insertionPoint = line.Offset + line.GetIndentation(textEditorData.Document).Length;
                        textEditorData.Insert(insertionPoint, "/// ");
                        return(true);
                    }
                    //multi-line comments
                }
                else if (stateTracker.Engine.IsInsideMultiLineComment)
                {
                    if (textEditorData.GetTextAt(line.Offset, line.Length).TrimStart().StartsWith("*", StringComparison.Ordinal))
                    {
                        return(false);
                    }
                    textEditorData.EnsureCaretIsNotVirtual();
                    string commentPrefix = string.Empty;
                    if (trimmedPreviousLine.StartsWith("* ", StringComparison.Ordinal))
                    {
                        commentPrefix = "* ";
                    }
                    else if (trimmedPreviousLine.StartsWith("/**", StringComparison.Ordinal) || trimmedPreviousLine.StartsWith("/*", StringComparison.Ordinal))
                    {
                        commentPrefix = " * ";
                    }
                    else if (trimmedPreviousLine.StartsWith("*", StringComparison.Ordinal))
                    {
                        commentPrefix = "*";
                    }

                    int indentSize   = line.GetIndentation(textEditorData.Document).Length;
                    var insertedText = prevLine.GetIndentation(textEditorData.Document) + commentPrefix;
                    textEditorData.Replace(line.Offset, indentSize, insertedText);
                    textEditorData.Caret.Offset = line.Offset + insertedText.Length;
                    return(true);
                }
                else if (stateTracker.Engine.IsInsideStringLiteral)
                {
                    var lexer = new CSharpCompletionEngineBase.MiniLexer(textEditorData.Document.GetTextAt(0, prevLine.EndOffset));
                    lexer.Parse();
                    if (!lexer.IsInString)
                    {
                        return(false);
                    }
                    textEditorData.EnsureCaretIsNotVirtual();
                    textEditorData.Insert(prevLine.Offset + prevLine.Length, "\" +");

                    int indentSize   = line.GetIndentation(textEditorData.Document).Length;
                    var insertedText = prevLine.GetIndentation(textEditorData.Document) + (trimmedPreviousLine.StartsWith("\"", StringComparison.Ordinal) ? "" : "\t") + "\"";
                    textEditorData.Replace(line.Offset, indentSize, insertedText);
                    return(true);
                }
            }
            return(false);
        }
		void InitTracker ()
		{
			//if there's a CSharpTextEditorIndentation in the extension chain, we can reuse its stateTracker
			CSharpTextEditorIndentation c = this.Document.GetContent<CSharpTextEditorIndentation> ();
			if (c != null && c.StateTracker != null) {
				stateTracker = c.StateTracker;
			} else {
				stateTracker = new DocumentStateTracker<CSharpIndentEngine> (new CSharpIndentEngine (policy), textEditorData);
			}
		}
		public IndentVirtualSpaceManager (Mono.TextEditor.TextEditorData data, DocumentStateTracker<CSharpIndentEngine> stateTracker)
		{
			this.data = data;
			this.stateTracker = stateTracker;
		}
		public static bool FixLineStart (TextEditorData textEditorData, DocumentStateTracker<CSharpIndentEngine> stateTracker, int lineNumber)
		{
			if (lineNumber > DocumentLocation.MinLine) {
				DocumentLine line = textEditorData.Document.GetLine (lineNumber);
				if (line == null)
					return false;

				DocumentLine prevLine = textEditorData.Document.GetLine (lineNumber - 1);
				if (prevLine == null)
					return false;
				string trimmedPreviousLine = textEditorData.Document.GetTextAt (prevLine).TrimStart ();

				//xml doc comments
				//check previous line was a doc comment
				//check there's a following line?
				if (trimmedPreviousLine.StartsWith ("///", StringComparison.Ordinal)) {
					if (textEditorData.GetTextAt (line.Offset, line.Length).TrimStart ().StartsWith ("///", StringComparison.Ordinal))
						return false;
					//check that the newline command actually inserted a newline
					textEditorData.EnsureCaretIsNotVirtual ();
					string nextLine = textEditorData.Document.GetTextAt (textEditorData.Document.GetLine (lineNumber + 1)).TrimStart ();

					if (trimmedPreviousLine.Length > "///".Length || nextLine.StartsWith ("///", StringComparison.Ordinal)) {
						var insertionPoint = line.Offset + line.GetIndentation (textEditorData.Document).Length;
						textEditorData.Insert (insertionPoint, "/// ");
						return true;
					}
					//multi-line comments
				} else if (stateTracker.Engine.IsInsideMultiLineComment) {
					if (textEditorData.GetTextAt (line.Offset, line.Length).TrimStart ().StartsWith ("*", StringComparison.Ordinal))
						return false;
					textEditorData.EnsureCaretIsNotVirtual ();
					string commentPrefix = string.Empty;
					if (trimmedPreviousLine.StartsWith ("* ", StringComparison.Ordinal)) {
						commentPrefix = "* ";
					} else if (trimmedPreviousLine.StartsWith ("/**", StringComparison.Ordinal) || trimmedPreviousLine.StartsWith ("/*", StringComparison.Ordinal)) {
						commentPrefix = " * ";
					} else if (trimmedPreviousLine.StartsWith ("*", StringComparison.Ordinal)) {
						commentPrefix = "*";
					}

					int indentSize = line.GetIndentation (textEditorData.Document).Length;
					var insertedText = prevLine.GetIndentation (textEditorData.Document) + commentPrefix;
					textEditorData.Replace (line.Offset, indentSize, insertedText);
					textEditorData.Caret.Offset = line.Offset + insertedText.Length;
					return true;
				} else if (stateTracker.Engine.IsInsideStringLiteral) {
					var lexer = new CSharpCompletionEngineBase.MiniLexer (textEditorData.Document.GetTextAt (0, prevLine.EndOffset));
					lexer.Parse ();
					if (!lexer.IsInString)
						return false;
					textEditorData.EnsureCaretIsNotVirtual ();
					textEditorData.Insert (prevLine.Offset + prevLine.Length, "\" +");

					int indentSize = line.GetIndentation (textEditorData.Document).Length;
					var insertedText = prevLine.GetIndentation (textEditorData.Document) + (trimmedPreviousLine.StartsWith ("\"", StringComparison.Ordinal) ? "" : "\t") + "\"";
					textEditorData.Replace (line.Offset, indentSize, insertedText);
					return true;
				}
			}
			return false;
		}
示例#42
0
 public IndentVirtualSpaceManager(Mono.TextEditor.TextEditorData data, DocumentStateTracker <CSharpIndentEngine> stateTracker)
 {
     this.data         = data;
     this.stateTracker = stateTracker;
 }
		protected override void Initialize ()
		{
			base.Initialize ();

			// Delay the execution of UpdateOwnerProjects since it may end calling Document.AttachToProject,
			// which shouldn't be called while the extension chain is being initialized.
			// TODO: Move handling of owner projects to Document
			Application.Invoke (delegate {
				UpdateOwnerProjects ();
			});

			var parser = new XmlParser (CreateRootState (), false);
			tracker = new DocumentStateTracker<XmlParser> (parser, Editor);
			DocumentContext.DocumentParsed += UpdateParsedDocument;
			Editor.CaretPositionChanged += HandleCaretPositionChanged;

			if (DocumentContext.ParsedDocument != null) {
				lastCU = DocumentContext.ParsedDocument;
				OnParsedDocumentUpdated ();
			}

			if (IdeApp.Workspace != null) {
				IdeApp.Workspace.FileAddedToProject += HandleProjectChanged;
				IdeApp.Workspace.FileRemovedFromProject += HandleProjectChanged;
			}
		}
示例#44
0
        byte[] ITextPasteHandler.GetCopyData(TextSegment segment)
        {
            stateTracker.UpdateEngine(segment.Offset);
            if (stateTracker.Engine.IsInsideStringLiteral)
            {
                return new [] { (byte)CopySource.StringLiteral }
            }
            ;
            if (stateTracker.Engine.IsInsideVerbatimString)
            {
                return new [] { (byte)CopySource.VerbatimString }
            }
            ;
            return(null);
        }

        string ITextPasteHandler.FormatPlainText(int insertionOffset, string text, byte[] copyData)
        {
            if (document.Editor.Options.IndentStyle == IndentStyle.None ||
                document.Editor.Options.IndentStyle == IndentStyle.Auto)
            {
                return(text);
            }

            if (copyData != null && copyData.Length == 1)
            {
                CopySource src = (CopySource)copyData [0];
                switch (src)
                {
                case CopySource.VerbatimString:
                    text = text.Replace("\"\"", "\"");
                    break;

                case CopySource.StringLiteral:
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < text.Length; i++)
                    {
                        switch (text [i])
                        {
                        case '\\':
                            i++;
                            switch (text [i])
                            {
                            case '\\':
                                sb.Append('\\');
                                break;

                            case 'r':
                                sb.Append('\r');
                                break;

                            case 'n':
                                sb.Append('\n');
                                break;

                            case 't':
                                sb.Append('\t');
                                break;
                            }
                            break;

                        default:
                            sb.Append(text [i]);
                            break;
                        }
                    }
                    text = sb.ToString();
                    break;
                }
            }

            stateTracker.UpdateEngine(insertionOffset);
            var engine = stateTracker.Engine.Clone() as CSharpIndentEngine;

            StringBuilder result = new StringBuilder();

            if (engine.IsInsideStringLiteral)
            {
                foreach (var ch in text)
                {
                    switch (ch)
                    {
                    case '\t':
                        result.Append("\\t");
                        break;

                    case '"':
                        result.Append("\\\"");
                        break;

                    case '\n':
                        result.Append("\\n");
                        break;

                    case '\r':
                        result.Append("\\r");
                        break;

                    case '\\':
                        result.Append("\\\\");
                        break;

                    default:
                        result.Append(ch);
                        break;
                    }
                }
                return(result.ToString());
            }


            if (engine.IsInsideVerbatimString)
            {
                foreach (var ch in text)
                {
                    switch (ch)
                    {
                    case '"':
                        result.Append("\"\"");
                        break;

                    default:
                        result.Append(ch);
                        break;
                    }
                }
                return(result.ToString());
            }

            bool inNewLine = false;

            foreach (var ch in text)
            {
                if (!engine.IsInsideOrdinaryCommentOrString)
                {
                    if (inNewLine && (ch == ' ' || ch == '\t'))
                    {
                        engine.Push(ch);
                        continue;
                    }
                }

                if (inNewLine && ch != '\n' && ch != '\r')
                {
                    if (!engine.IsInsideOrdinaryCommentOrString)
                    {
                        if (ch != '#')
                        {
                            engine.Push(ch);
                        }
                        result.Append(engine.ThisLineIndent);
                        if (ch == '#')
                        {
                            engine.Push(ch);
                        }
                    }
                    inNewLine = false;
                }
                else
                {
                    engine.Push(ch);
                }
                result.Append(ch);
                if (ch == '\n' || ch == '\r')
                {
                    inNewLine = true;
                }
            }
            return(result.ToString());
        }

        #endregion

        #region Sharing the tracker

        void InitTracker()
        {
            stateTracker = new DocumentStateTracker <CSharpIndentEngine> (new CSharpIndentEngine(Policy, TextStylePolicy), textEditorData);
        }