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 <PlayScriptFormattingPolicy> (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(); }
string GetIndentationString(int offset, DocumentLocation loc) { stateTracker.Update(Math.Min(data.Length, offset)); DocumentLine line = data.Document.GetLine(loc.Line); if (line == null) { return(""); } // Get context to the end of the line w/o changing the main engine's state var ctx = stateTracker.Clone(); ctx.Update(line.Offset + line.Length); // int pos = line.Offset; string curIndent = line.GetIndentation(data.Document); int nlwsp = curIndent.Length; // int o = offset > pos + nlwsp ? offset - (pos + nlwsp) : 0; if (!stateTracker.LineBeganInsideMultiLineComment || (nlwsp < line.LengthIncludingDelimiter && data.Document.GetCharAt(line.Offset + nlwsp) == '*')) { return(ctx.ThisLineIndent); } return(curIndent); }
void DoReSmartIndent(int cursor) { if (stateTracker.Engine.LineBeganInsideVerbatimString || stateTracker.Engine.LineBeganInsideMultiLineComment) { return; } DocumentLine line = textEditorData.Document.GetLineByOffset(cursor); // stateTracker.UpdateEngine (line.Offset); // Get context to the end of the line w/o changing the main engine's state var ctx = (CSharpIndentEngine)stateTracker.Engine.Clone(); for (int max = cursor; max < line.EndOffset; max++) { ctx.Push(textEditorData.Document.GetCharAt(max)); } int pos = line.Offset; string curIndent = line.GetIndentation(textEditorData.Document); int nlwsp = curIndent.Length; int offset = cursor > pos + nlwsp ? cursor - (pos + nlwsp) : 0; if (!stateTracker.Engine.LineBeganInsideMultiLineComment || (nlwsp < line.LengthIncludingDelimiter && textEditorData.Document.GetCharAt(line.Offset + nlwsp) == '*')) { // Possibly replace the indent string newIndent = ctx.ThisLineIndent; int newIndentLength = newIndent.Length; if (newIndent != curIndent) { if (CompletionWindowManager.IsVisible) { if (pos < CompletionWindowManager.CodeCompletionContext.TriggerOffset) { CompletionWindowManager.CodeCompletionContext.TriggerOffset -= nlwsp; } } newIndentLength = textEditorData.Replace(pos, nlwsp, newIndent); textEditorData.Document.CommitLineUpdate(textEditorData.Caret.Line); // Engine state is now invalid stateTracker.ResetEngineToPosition(pos); CompletionWindowManager.HideWindow(); } pos += newIndentLength; } else { pos += curIndent.Length; } pos += offset; textEditorData.FixVirtualIndentation(); }
public string GetIndentationString(int lineNumber, int column) { DocumentLine line = doc.GetLine(lineNumber - 1); while (line != null) { var indent = line.GetIndentation(doc); if (indent.Length > 0) { return(indent); } line = line.PreviousLine; } return(""); }
public static int GetIndentLength(TextDocument doc, int offset, int length, bool skipFirstLine) { int curOffset = offset; int indentLength = int.MaxValue; while (curOffset < offset + length) { DocumentLine line = doc.GetLineByOffset(curOffset); if (!skipFirstLine) { indentLength = System.Math.Min(indentLength, line.GetIndentation(doc).Length); } else { skipFirstLine = false; } curOffset = line.EndOffsetIncludingDelimiter + 1; } return(indentLength == int.MaxValue ? 0 : indentLength); }
public override void CorrectIndenting(PolicyContainer policyParent, IEnumerable <string> mimeTypeChain, TextEditorData data, int line) { DocumentLine lineSegment = data.Document.GetLine(line); if (lineSegment == null) { return; } try { var policy = policyParent.Get <CSharpFormattingPolicy> (mimeTypeChain); var tracker = new CSharpIndentEngine(data.Document, data.CreateNRefactoryTextEditorOptions(), policy.CreateOptions()); tracker.Update(lineSegment.Offset); for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.Length; i++) { tracker.Push(data.Document.GetCharAt(i)); } string curIndent = lineSegment.GetIndentation(data.Document); int nlwsp = curIndent.Length; if (!tracker.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.LengthIncludingDelimiter && data.Document.GetCharAt(lineSegment.Offset + nlwsp) == '*')) { // Possibly replace the indent string newIndent = tracker.ThisLineIndent; if (newIndent != curIndent) { data.Replace(lineSegment.Offset, nlwsp, newIndent); } } } catch (Exception e) { LoggingService.LogError("Error while indenting", e); } }
// void ResultLineDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) // { // if (TreeIter.Zero.Equals (iter)) // return; // var lineRenderer = (CellRendererText)cell; // var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn); // if (searchResult == null) // return; // // Document doc = GetDocument (searchResult); // int lineNr = doc.OffsetToLineNumber (searchResult.Offset) + 1; // bool didRead = (bool)store.GetValue (iter, DidReadColumn); // lineRenderer.Markup = MarkupText (lineNr.ToString (), didRead); // } // void ResultTextDataFunc(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) { if (TreeIter.Zero.Equals(iter)) { return; } var textRenderer = (CellRendererText)cell; var searchResult = (SearchResult)store.GetValue(iter, SearchResultColumn); if (searchResult == null || searchResult.Offset < 0) { textRenderer.Markup = "Invalid search result"; return; } var doc = GetDocument(searchResult); if (doc == null) { textRenderer.Markup = "Can't create document for:" + searchResult.FileName; return; } bool isSelected = treeviewSearchResults.Selection.IterIsSelected(iter); if (searchResult.Markup == null) { if (searchResult.LineNumber <= 0) { searchResult.LineNumber = doc.OffsetToLineNumber(searchResult.Offset); } DocumentLine line = doc.GetLine(searchResult.LineNumber); if (line == null) { textRenderer.Markup = "Invalid line number " + searchResult.LineNumber + " from offset: " + searchResult.Offset; return; } int indent = line.GetIndentation(doc).Length; var data = new Mono.TextEditor.TextEditorData(doc); data.ColorStyle = highlightStyle; var lineText = doc.GetTextAt(line.Offset + indent, line.Length - indent); int col = searchResult.Offset - line.Offset - indent; // search result contained part of the indent. if (col + searchResult.Length < lineText.Length) { lineText = doc.GetTextAt(line.Offset, line.Length); } var markup = doc.SyntaxMode != null? data.GetMarkup(line.Offset + indent, line.Length - indent, true, !isSelected, false) : GLib.Markup.EscapeText(lineText); searchResult.Markup = AdjustColors(markup.Replace("\t", new string (' ', TextEditorOptions.DefaultOptions.TabSize))); uint start; uint end; try { start = (uint)TextViewMargin.TranslateIndexToUTF8(lineText, col); end = (uint)TextViewMargin.TranslateIndexToUTF8(lineText, col + searchResult.Length); } catch (Exception e) { LoggingService.LogError("Exception while translating index to utf8 (column was:" + col + " search result length:" + searchResult.Length + " line text:" + lineText + ")", e); return; } searchResult.StartIndex = start; searchResult.EndIndex = end; } try { textRenderer.Markup = searchResult.Markup; if (!isSelected) { var searchColor = highlightStyle.SearchResult.Color; double b1 = Mono.TextEditor.HslColor.Brightness(searchColor); double b2 = Mono.TextEditor.HslColor.Brightness(AdjustColor(Style.Base(StateType.Normal), (Mono.TextEditor.HslColor)highlightStyle.PlainText.Foreground)); double delta = Math.Abs(b1 - b2); if (delta < 0.1) { Mono.TextEditor.HslColor color1 = highlightStyle.SearchResult.Color; if (color1.L + 0.5 > 1.0) { color1.L -= 0.5; } else { color1.L += 0.5; } searchColor = color1; } var attr = new Pango.AttrBackground((ushort)(searchColor.R * ushort.MaxValue), (ushort)(searchColor.G * ushort.MaxValue), (ushort)(searchColor.B * ushort.MaxValue)); attr.StartIndex = searchResult.StartIndex; attr.EndIndex = searchResult.EndIndex; using (var list = textRenderer.Attributes.Copy()) { list.Insert(attr); textRenderer.Attributes = list; } } } catch (Exception e) { LoggingService.LogError("Error whil setting the text renderer markup to: " + searchResult.Markup, e); } }
public bool FixLineStart (TextEditorData textEditorData, IStateMachineIndentEngine 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 (); var nextLineSegment = textEditorData.Document.GetLine (lineNumber + 1); string nextLine = nextLineSegment != null ? textEditorData.Document.GetTextAt (nextLineSegment).TrimStart () : ""; if (trimmedPreviousLine.Length > "///".Length || nextLine.StartsWith ("///", StringComparison.Ordinal)) { var insertionPoint = textEditorData.Caret.Offset; int inserted = textEditorData.Insert (insertionPoint, "/// "); textEditorData.Caret.Offset = insertionPoint + inserted; return true; } //multi-line comments } else if (stateTracker.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 (wasInStringLiteral) { 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 ResultTextDataFunc(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) { if (TreeIter.Zero.Equals(iter)) { return; } var textRenderer = (CellRendererText)cell; var searchResult = (SearchResult)store.GetValue(iter, SearchResultColumn); if (searchResult == null || searchResult.Offset < 0) { textRenderer.Markup = "Invalid search result"; return; } string textMarkup = searchResult.TextMarkup; if (textMarkup == null) { var doc = GetDocument(searchResult); if (doc == null) { textMarkup = "Can't create document for:" + searchResult.FileName; goto end; } bool isSelected = treeviewSearchResults.Selection.IterIsSelected(iter); if (searchResult.Markup == null) { if (searchResult.LineNumber <= 0) { searchResult.LineNumber = doc.OffsetToLineNumber(searchResult.Offset); } DocumentLine line = doc.GetLine(searchResult.LineNumber); if (line == null) { textMarkup = "Invalid line number " + searchResult.LineNumber + " from offset: " + searchResult.Offset; goto end; } int indent = line.GetIndentation(doc).Length; var data = new Mono.TextEditor.TextEditorData(doc); data.ColorStyle = highlightStyle; var lineText = doc.GetTextAt(line.Offset + indent, line.Length - indent); int col = searchResult.Offset - line.Offset - indent; // search result contained part of the indent. if (col + searchResult.Length < lineText.Length) { lineText = doc.GetTextAt(line.Offset, line.Length); } var markup = doc.SyntaxMode != null? data.GetMarkup(line.Offset + indent, line.Length - indent, true, !isSelected, false) : GLib.Markup.EscapeText(lineText); searchResult.Markup = AdjustColors(markup.Replace("\t", new string (' ', TextEditorOptions.DefaultOptions.TabSize))); if (col >= 0) { uint start; uint end; try { start = (uint)TextViewMargin.TranslateIndexToUTF8(lineText, col); end = (uint)TextViewMargin.TranslateIndexToUTF8(lineText, Math.Min(lineText.Length, col + searchResult.Length)); } catch (Exception e) { LoggingService.LogError("Exception while translating index to utf8 (column was:" + col + " search result length:" + searchResult.Length + " line text:" + lineText + ")", e); return; } searchResult.StartIndex = start; searchResult.EndIndex = end; } } try { textMarkup = searchResult.Markup; if (!isSelected) { var searchColor = searchResult.GetBackgroundMarkerColor(highlightStyle).Color; double b1 = Mono.TextEditor.HslColor.Brightness(searchColor); double b2 = Mono.TextEditor.HslColor.Brightness(AdjustColor(Style.Base(StateType.Normal), (Mono.TextEditor.HslColor)highlightStyle.PlainText.Foreground)); double delta = Math.Abs(b1 - b2); if (delta < 0.1) { Mono.TextEditor.HslColor color1 = highlightStyle.SearchResult.Color; if (color1.L + 0.5 > 1.0) { color1.L -= 0.5; } else { color1.L += 0.5; } searchColor = color1; } if (searchResult.StartIndex != searchResult.EndIndex) { var markupBuilder = new StringBuilder(); bool inMarkup = false, inEntity = false, closed = false; int i = 0; for (int j = 0; j < textMarkup.Length; j++) { var ch = textMarkup [j]; if (inEntity) { if (ch == ';') { inEntity = false; } markupBuilder.Append(ch); continue; } if (inMarkup) { if (ch == '>') { inMarkup = false; } markupBuilder.Append(ch); continue; } if (i == searchResult.EndIndex) { markupBuilder.Append("</span>"); markupBuilder.Append(textMarkup.Substring(j)); closed = true; break; } if (ch == '&') { inEntity = true; markupBuilder.Append(ch); continue; } if (ch == '<') { inMarkup = true; markupBuilder.Append(ch); continue; } if (i == searchResult.StartIndex) { markupBuilder.Append("<span background=\"" + ColorToPangoMarkup((HslColor)searchColor) + "\">"); } markupBuilder.Append(ch); i++; } if (!closed) { markupBuilder.Append("</span>"); } textMarkup = markupBuilder.ToString(); } } } catch (Exception e) { LoggingService.LogError("Error whil setting the text renderer markup to: " + searchResult.Markup, e); } end: searchResult.TextMarkup = textMarkup; } textRenderer.Markup = textMarkup; }
// void ResultLineDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) // { // if (TreeIter.Zero.Equals (iter)) // return; // var lineRenderer = (CellRendererText)cell; // var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn); // if (searchResult == null) // return; // // Document doc = GetDocument (searchResult); // int lineNr = doc.OffsetToLineNumber (searchResult.Offset) + 1; // bool didRead = (bool)store.GetValue (iter, DidReadColumn); // lineRenderer.Markup = MarkupText (lineNr.ToString (), didRead); // } // void ResultTextDataFunc(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) { if (TreeIter.Zero.Equals(iter)) { return; } var textRenderer = (CellRendererText)cell; var searchResult = (SearchResult)store.GetValue(iter, SearchResultColumn); if (searchResult == null || searchResult.Offset < 0) { textRenderer.Markup = "Invalid search result"; return; } var doc = GetDocument(searchResult); if (doc == null) { textRenderer.Markup = "Can't create document for:" + searchResult.FileName; return; } int lineNr = doc.OffsetToLineNumber(searchResult.Offset); DocumentLine line = doc.GetLine(lineNr); if (line == null) { textRenderer.Markup = "Invalid line number " + lineNr + " from offset: " + searchResult.Offset; return; } bool isSelected = treeviewSearchResults.Selection.IterIsSelected(iter); int indent = line.GetIndentation(doc).Length; var data = new Mono.TextEditor.TextEditorData(doc); data.ColorStyle = highlightStyle; string markup = doc.SyntaxMode != null? data.GetMarkup(line.Offset + indent, line.Length - indent, true, !isSelected, false) : GLib.Markup.EscapeText(doc.GetTextAt(line.Offset, line.Length)); if (!isSelected) { int col = searchResult.Offset - line.Offset - indent; string tag; int pos1 = FindPosition(markup, col, out tag); int pos2 = FindPosition(markup, col + searchResult.Length, out tag); if (pos1 >= 0 && pos2 >= 0) { markup = tag.StartsWith("span") ? markup.Insert(pos2, "</span></span><" + tag + ">") : markup.Insert(pos2, "</span>"); Color searchColor = Mono.TextEditor.Highlighting.ColorScheme.ToGdkColor(highlightStyle.SearchTextBg); double b1 = Mono.TextEditor.HslColor.Brightness(searchColor); double b2 = Mono.TextEditor.HslColor.Brightness(AdjustColor(Style.Base(StateType.Normal), highlightStyle.Default.Color)); double delta = Math.Abs(b1 - b2); if (delta < 0.1) { Mono.TextEditor.HslColor color1 = highlightStyle.SearchTextBg; if (color1.L + 0.5 > 1.0) { color1.L -= 0.5; } else { color1.L += 0.5; } searchColor = color1; } markup = markup.Insert(pos1, "<span background=\"" + SyntaxMode.ColorToPangoMarkup(searchColor) + "\">"); } } string markupText = AdjustColors(markup.Replace("\t", new string (' ', TextEditorOptions.DefaultOptions.TabSize))); try { textRenderer.Markup = markupText; } catch (Exception e) { LoggingService.LogError("Error whil setting the text renderer markup to: " + markup, e); } }