public void CopySelection() { TreeModel model; StringBuilder sb = new StringBuilder(); foreach (Gtk.TreePath p in treeviewSearchResults.Selection.GetSelectedRows(out model)) { TreeIter iter; if (!model.GetIter(out iter, p)) { continue; } SearchResult result = store.GetValue(iter, SearchResultColumn) as SearchResult; if (result == null) { continue; } DocumentLocation loc = GetLocation(result); Mono.TextEditor.Document doc = GetDocument(result); LineSegment line = doc.GetLine(loc.Line - 1); sb.AppendFormat("{0} ({1}, {2}):{3}", result.FileName, loc.Line, loc.Column, doc.GetTextAt(line.Offset, line.EditableLength)); sb.AppendLine(); } Gtk.Clipboard clipboard = Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false)); clipboard.Text = sb.ToString(); clipboard = Clipboard.Get(Gdk.Atom.Intern("PRIMARY", false)); clipboard.Text = sb.ToString(); }
static string GetIndent(string text) { Mono.TextEditor.Document doc = new Mono.TextEditor.Document(); doc.Text = text; StringBuilder result = null; for (int i = 1; i < doc.LineCount; i++) { LineSegment line = doc.GetLine(i); StringBuilder lineIndent = new StringBuilder(); foreach (char ch in doc.GetTextAt(line)) { if (!char.IsWhiteSpace(ch)) { break; } lineIndent.Append(ch); } if (line.EditableLength == lineIndent.Length) { continue; } if (result == null || lineIndent.Length < result.Length) { result = lineIndent; } } if (result == null) { return(""); } return(result.ToString()); }
DocumentLocation GetLocation(SearchResult searchResult) { Mono.TextEditor.Document doc = GetDocument(searchResult); int lineNr = doc.OffsetToLineNumber(searchResult.Offset); LineSegment line = doc.GetLine(lineNr); return(new DocumentLocation(lineNr + 1, searchResult.Offset - line.Offset + 1)); }
int FindNextWordOffset (Document doc, int offset, bool subword) { int lineNumber = doc.OffsetToLineNumber (offset); LineSegment line = doc.GetLine (lineNumber); if (line == null) return offset; int result = offset; int endOffset = line.Offset + line.EditableLength; if (result == endOffset) { line = doc.GetLine (lineNumber + 1); if (line != null) result = line.Offset; return result; } CharacterClass current = GetCharacterClass (doc.GetCharAt (result), subword, false); while (result < endOffset) { CharacterClass next = GetCharacterClass (doc.GetCharAt (result), subword, false); if (next != current) { // camelCase and PascalCase handling bool camelSkip = false; if (next == CharacterClass.LowercaseLetter && current == CharacterClass.UppercaseLetter) { if (result-2 > line.Offset) { CharacterClass previous = GetCharacterClass (doc.GetCharAt (result-2), subword, false); if (previous == CharacterClass.UppercaseLetter && result-2 > offset) result--; else camelSkip = true; } } if (!camelSkip) break; } current = next; result++; } while (result < endOffset && GetCharacterClass (doc.GetCharAt (result), subword, false) == CharacterClass.Whitespace) { result++; } return result; }
static int GetPrevOffset (Document document, int lineNumber) { LineSegment startLine = document.GetLine (lineNumber); RedBlackTree<LineSegmentTree.TreeNode>.RedBlackTreeIterator iter = startLine.Iter; while (iter.MoveBack ()) { LineSegment line = iter.Current; if (line.IsBookmarked) { return line.Offset; } } return -1; }
static int GetNextOffset (Document document, int lineNumber) { int startLineNumber = lineNumber + 1; if (startLineNumber > document.Length) startLineNumber = 0; LineSegment startLine = document.GetLine (startLineNumber); RedBlackTree<LineSegmentTree.TreeNode>.RedBlackTreeIterator iter = startLine.Iter; do { LineSegment line = iter.Current; if (line.IsBookmarked) return line.Offset; } while (iter.MoveNext ()); return -1; }
static string StripDoubleBlankLines(string content) { var doc = new Mono.TextEditor.Document(content); for (int i = 1; i + 1 <= doc.LineCount; i++) { if (IsBlankLine(doc, i) && IsBlankLine(doc, i + 1)) { ((IBuffer)doc).Remove(doc.GetLine(i)); i--; continue; } } return(doc.Text); }
int FindPrevWordOffset (Document doc, int offset, bool subword) { int lineNumber = doc.OffsetToLineNumber (offset); LineSegment line = doc.GetLine (lineNumber); if (line == null) return offset; int result = offset; if (result == line.Offset) { line = doc.GetLine (lineNumber - 1); if (line != null) result = line.Offset + line.EditableLength; return result; } CharacterClass current = GetCharacterClass (doc.GetCharAt (result - 1), subword, false); if (current == CharacterClass.Whitespace && result - 1 > line.Offset) { result--; current = GetCharacterClass (doc.GetCharAt (result - 2), subword, false); } while (result > line.Offset) { CharacterClass prev = GetCharacterClass (doc.GetCharAt (result - 1), subword, false); if (prev != current) { // camelCase and PascalCase handling bool camelSkip = false; if (prev == CharacterClass.UppercaseLetter && current == CharacterClass.LowercaseLetter) { if (result-2 > line.Offset) { CharacterClass back2 = GetCharacterClass (doc.GetCharAt (result-2), subword, false); if (back2 == CharacterClass.UppercaseLetter) result--; else camelSkip = true; } } if (!camelSkip) break; } current = prev; result--; } return result; }
static string GetIndent(string text) { Mono.TextEditor.Document doc = new Mono.TextEditor.Document(); doc.Text = text; string result = null; for (int i = 1; i < doc.LineCount; i++) { string lineIndent = doc.GetLineIndent(i); if (doc.GetLine(i).EditableLength == lineIndent.Length) { continue; } if (result == null || lineIndent.Length < result.Length) { result = lineIndent; } } return(result ?? ""); }
static string StripHeader(string content) { var doc = new Mono.TextEditor.Document(content); while (true) { string lineText = doc.GetLineText(1); if (lineText == null) { break; } if (lineText.StartsWith("//")) { ((IBuffer)doc).Remove(doc.GetLine(1)); continue; } break; } return(doc.Text); }
static void CheckStartPoint (Document doc, InsertionPoint point, bool isEndPoint) { LineSegment line = doc.GetLine (point.Location.Line); if (line == null) return; if (doc.GetLineIndent (line).Length + 1 == point.Location.Column) { int lineNr = point.Location.Line; while (lineNr > 1 && doc.GetLineIndent (lineNr - 1).Length == doc.GetLine (lineNr - 1).EditableLength) { lineNr--; } line = doc.GetLine (lineNr); point.Location = new DocumentLocation (lineNr, doc.GetLineIndent (line).Length + 1); } if (doc.GetLineIndent (line).Length + 1 < point.Location.Column) point.LineBefore = isEndPoint ? NewLineInsertion.Eol : NewLineInsertion.BlankLine; if (point.Location.Column < line.EditableLength + 1) point.LineAfter = isEndPoint ? NewLineInsertion.Eol : NewLineInsertion.BlankLine; }
static void CheckEndPoint (Document doc, InsertionPoint point, bool isStartPoint) { LineSegment line = doc.GetLine (point.Location.Line); if (line == null) return; if (doc.GetLineIndent (line).Length + 1 < point.Location.Column) point.LineBefore = NewLineInsertion.BlankLine; if (point.Location.Column < line.EditableLength + 1) point.LineAfter = NewLineInsertion.Eol; }
static bool IsBlankLine (Document doc, int i) { var line = doc.GetLine (i); return line.EditableLength == line.GetIndentation (doc).Length; }
static string StripHeader (string content) { var doc = new Mono.TextEditor.Document (content); while (true) { string lineText = doc.GetLineText (1); if (lineText == null) break; if (lineText.StartsWith ("//")) { ((IBuffer)doc).Remove (doc.GetLine (1)); continue; } break; } return doc.Text; }
static string StripDoubleBlankLines (string content) { var doc = new Mono.TextEditor.Document (content); for (int i = 1; i + 1 <= doc.LineCount; i++) { if (IsBlankLine (doc, i) && IsBlankLine (doc, i + 1)) { ((IBuffer)doc).Remove (doc.GetLine (i)); i--; continue; } } return doc.Text; }
static InsertionPoint GetInsertionPosition (Document doc, int line, int column) { LineSegment nextLine = doc.GetLine (line + 1); int bodyEndOffset = doc.LocationToOffset (line, column) + 1; int endOffset = nextLine != null ? nextLine.Offset : doc.Length; for (int i = bodyEndOffset; i < endOffset; i++) { char ch = doc.GetCharAt (i); if (!char.IsWhiteSpace (ch)) return new InsertionPoint (doc.OffsetToLocation (i), true, true); } if (nextLine == null) return new InsertionPoint (doc.OffsetToLocation (bodyEndOffset - 1), true, true); int oldLine = line; bool curLineEmpty = false; if (doc.GetLineIndent (nextLine).Length == nextLine.EditableLength) { curLineEmpty = true; while (line + 2 < doc.LineCount && doc.GetLineIndent (line + 2).Length == doc.GetLine (line + 2).EditableLength) line++; } bool insertBefore = !curLineEmpty && line - oldLine <= 1; bool insertAfter = line - oldLine == 0; // if (curLineEmpty) // line++; int lineNumber = line + 1; return new InsertionPoint (new DocumentLocation (lineNumber, doc.GetLineIndent (lineNumber).Length), insertBefore, insertAfter); }
public static void Format (TextEditorData data, IType type, IMember member, ProjectDom dom, ICompilationUnit unit, DomLocation caretLocation) { if (type == null) return; if (member == null) { // member = type; return; } string wrapper; int endPos; wrapper = CreateWrapperClassForMember (member, member != type, data, out endPos); if (string.IsNullOrEmpty (wrapper) || endPos < 0) return; // Console.WriteLine (wrapper); int bracketIndex = wrapper.IndexOf ('{') + 1; int col = GetColumn (wrapper, bracketIndex, data.Options.TabSize); CSharpFormatter formatter = new CSharpFormatter (); formatter.startIndentLevel = System.Math.Max (0, col / data.Options.TabSize - 1); string formattedText = formatter.InternalFormat (dom != null && dom.Project != null ? dom.Project.Policies : null, MimeType, wrapper, 0, wrapper.Length); if (formatter.hasErrors) return; int startPos = data.Document.LocationToOffset (member.Location.Line, 1) - 1; InFormat = true; if (member != type) { int len1 = formattedText.IndexOf ('{') + 1; int last = formattedText.LastIndexOf ('}'); formattedText = formattedText.Substring (len1, last - len1 - 1).TrimStart ('\n', '\r'); } else { startPos++; } //Console.WriteLine ("formattedText0:" + formattedText.Replace ("\t", "->").Replace (" ", "°")); if (member != type) { string indentToRemove = GetIndent (formattedText); // Console.WriteLine ("Remove:" + indentToRemove.Replace ("\t", "->").Replace (" ", "°")); formattedText = RemoveIndent (formattedText, indentToRemove); } else { formattedText = formattedText.TrimStart (); } //Console.WriteLine ("Indent:" + GetIndent (data, member.Location.Line - 1).Replace ("\t", "->").Replace (" ", "°")); //Console.WriteLine ("formattedText1:" + formattedText.Replace ("\t", "->").Replace (" ", "°")); formattedText = AddIndent (formattedText, GetIndent (data, member.Location.Line)); Document doc = new Document (); doc.Text = formattedText; for (int i = doc.LineCount; i --> DocumentLocation.MinLine;) { LineSegment lineSegment = doc.GetLine (i); if (doc.IsEmptyLine (lineSegment)) ((IBuffer)doc).Remove (lineSegment.Offset, lineSegment.Length); } formattedText = doc.Text; //Console.WriteLine ("formattedText3:" + formattedText.Replace ("\t", "->").Replace (" ", "°").Replace ("\n", "\\n").Replace ("\r", "\\r")); int textLength = CanInsertFormattedText (data, startPos, data.Document.LocationToOffset (caretLocation.Line, caretLocation.Column), formattedText); if (textLength > 0) { // Console.WriteLine (formattedText.Substring (0, textLength)); InsertFormattedText (data, startPos, formattedText.Substring (0, textLength).TrimEnd ()); } else { Console.WriteLine ("Can't insert !!!"); } InFormat = false; }
void ResultTextDataFunc(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) { if (TreeIter.Zero.Equals(iter)) { return; } CellRendererText textRenderer = (CellRendererText)cell; SearchResult searchResult = (SearchResult)store.GetValue(iter, SearchResultColumn); if (searchResult == null) { return; } Mono.TextEditor.Document doc = GetDocument(searchResult); int lineNr = doc.OffsetToLineNumber(searchResult.Offset); LineSegment line = doc.GetLine(lineNr); bool isSelected = treeviewSearchResults.Selection.IterIsSelected(iter); string markup; if (doc.SyntaxMode != null) { markup = doc.SyntaxMode.GetMarkup(doc, new TextEditorOptions(), highlightStyle, line.Offset, line.EditableLength, true, !isSelected, false); } else { markup = GLib.Markup.EscapeText(doc.GetTextAt(line.Offset, line.EditableLength)); } if (!isSelected) { int col = searchResult.Offset - line.Offset; string tag; int pos1 = FindPosition(markup, col, out tag); int pos2 = FindPosition(markup, col + searchResult.Length, out tag); if (pos1 >= 0 && pos2 >= 0) { if (tag.StartsWith("span")) { markup = markup.Insert(pos2, "</span></span><" + tag + ">"); } else { markup = markup.Insert(pos2, "</span>"); } Gdk.Color searchColor = highlightStyle.SearchTextBg; double b1 = HslColor.Brightness(searchColor); double b2 = HslColor.Brightness(AdjustColor(Style.Base(StateType.Normal), highlightStyle.Default.Color)); double delta = Math.Abs(b1 - b2); if (delta < 0.1) { 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); } }
public static List<InsertionPoint> GetInsertionPoints (Document doc, IType type) { if (doc == null) throw new ArgumentNullException ("doc"); if (type == null) throw new ArgumentNullException ("type"); List<InsertionPoint> result = new List<InsertionPoint> (); int offset = doc.LocationToOffset (type.BodyRegion.Start.Line, type.BodyRegion.Start.Column); if (offset < 0) return result; while (offset < doc.Length && doc.GetCharAt (offset) != '{' && char.IsWhiteSpace (doc.GetCharAt (offset))) offset++; var realStartLocation = doc.OffsetToLocation (offset); result.Add (GetInsertionPosition (doc, realStartLocation.Line, realStartLocation.Column)); result[0].LineBefore = NewLineInsertion.None; foreach (IMember member in type.Members) { DomLocation domLocation = member.BodyRegion.End; if (domLocation.Line <= 0) { LineSegment lineSegment = doc.GetLine (member.Location.Line); if (lineSegment == null) continue; domLocation = new DomLocation (member.Location.Line, lineSegment.EditableLength + 1); } result.Add (GetInsertionPosition (doc, domLocation.Line, domLocation.Column)); } result[result.Count - 1].LineAfter = NewLineInsertion.None; CheckStartPoint (doc, result[0], result.Count == 1); if (result.Count > 1) CheckEndPoint (doc, result[result.Count - 1], result.Count == 1); return result; }
static InsertionPoint GetInsertionPosition (Document doc, int line, int column) { int bodyEndOffset = doc.LocationToOffset (line, column) + 1; LineSegment curLine = doc.GetLine (line); if (curLine != null) { if (bodyEndOffset < curLine.Offset + curLine.EditableLength) return new InsertionPoint (new DocumentLocation (line, column + 1), NewLineInsertion.BlankLine, NewLineInsertion.BlankLine); } LineSegment nextLine = doc.GetLine (line + 1); int endOffset = nextLine != null ? nextLine.Offset : doc.Length; for (int i = bodyEndOffset; i < endOffset; i++) { char ch = doc.GetCharAt (i); if (!char.IsWhiteSpace (ch)) return new InsertionPoint (doc.OffsetToLocation (i), NewLineInsertion.BlankLine, NewLineInsertion.BlankLine); } if (nextLine == null) return new InsertionPoint (doc.OffsetToLocation (bodyEndOffset - 1), NewLineInsertion.BlankLine, NewLineInsertion.BlankLine); int oldLine = line; while (line < doc.LineCount && doc.GetLineIndent (line + 1).Length == doc.GetLine (line + 1).EditableLength) line++; NewLineInsertion insertBefore = NewLineInsertion.None; NewLineInsertion insertAfter = NewLineInsertion.None; int delta = line - oldLine; int lineNumber = line + 1; if (delta == 0) { insertBefore = NewLineInsertion.Eol; insertAfter = NewLineInsertion.BlankLine; } else if (delta == 1) { insertAfter = NewLineInsertion.BlankLine; } else if (delta == 2) { lineNumber--; insertAfter = NewLineInsertion.BlankLine; } else if (delta >= 3) { lineNumber -= 2; insertAfter = NewLineInsertion.None; } return new InsertionPoint (new DocumentLocation (lineNumber, doc.GetLineIndent (lineNumber).Length + 1), insertBefore, insertAfter); }
public static List<InsertionPoint> GetInsertionPoints (Document doc, IType type) { if (doc == null) throw new ArgumentNullException ("doc"); if (type == null) throw new ArgumentNullException ("type"); List<InsertionPoint> result = new List<InsertionPoint> (); result.Add (GetInsertionPosition (doc, type.BodyRegion.Start.Line - 1, type.BodyRegion.Start.Column)); result[0].ShouldInsertNewLineBefore = false; foreach (IMember member in type.Members) { DomLocation domLocation = member.BodyRegion.End; if (domLocation.Line <= 0) { LineSegment lineSegment = doc.GetLine (member.Location.Line - 1); if (lineSegment == null) continue; domLocation = new DomLocation (member.Location.Line, lineSegment.EditableLength + 1); } result.Add (GetInsertionPosition (doc, domLocation.Line - 1, domLocation.Column - 1)); } result[result.Count - 1].ShouldInsertNewLineAfter = false; CheckStartPoint (doc, result[0]); if (result.Count > 1) CheckEndPoint (doc, result[result.Count - 1]); return result; }
static InsertionPoint GetInsertionPosition (Document doc, int line, int column) { int bodyEndOffset = doc.LocationToOffset (line, column) + 1; LineSegment curLine = doc.GetLine (line); if (curLine != null) { if (bodyEndOffset < curLine.Offset + curLine.EditableLength) { System.Console.WriteLine (1); // case1: positition is somewhere inside the start line return new InsertionPoint (new DocumentLocation (line, column + 1), NewLineInsertion.BlankLine, NewLineInsertion.BlankLine); } } // -> if position is at line end check next line LineSegment nextLine = doc.GetLine (line + 1); if (nextLine == null) // check for 1 line case. return new InsertionPoint (new DocumentLocation (line, column + 1), NewLineInsertion.BlankLine, NewLineInsertion.BlankLine); for (int i = nextLine.Offset; i < nextLine.EndOffset; i++) { char ch = doc.GetCharAt (i); if (!char.IsWhiteSpace (ch)) { // case2: next line contains non ws chars. System.Console.WriteLine (2); return new InsertionPoint (new DocumentLocation (line + 1, 1), NewLineInsertion.BlankLine, NewLineInsertion.BlankLine); } } // case3: whitespace line return new InsertionPoint (new DocumentLocation (line + 1, 1), NewLineInsertion.BlankLine, NewLineInsertion.None); }
static string GetIndent (string text) { Mono.TextEditor.Document doc = new Mono.TextEditor.Document (); doc.Text = text; string result = null; for (int i = 1; i < doc.LineCount; i++) { string lineIndent = doc.GetLineIndent (i); if (doc.GetLine (i).EditableLength == lineIndent.Length) continue; if (result == null || lineIndent.Length < result.Length) result = lineIndent; } return result ?? ""; }
static string GetIndent (string text) { Mono.TextEditor.Document doc = new Mono.TextEditor.Document (); doc.Text = text; StringBuilder result = null; for (int i = 1; i < doc.LineCount; i++) { LineSegment line = doc.GetLine (i); StringBuilder lineIndent = new StringBuilder (); foreach (char ch in doc.GetTextAt (line)) { if (!char.IsWhiteSpace (ch)) break; lineIndent.Append (ch); } if (line.EditableLength == lineIndent.Length) continue; if (result == null || lineIndent.Length < result.Length) result = lineIndent; } if (result == null) return ""; return result.ToString (); }
void UnderLineError (Document doc, Error info) { LineSegment line = doc.GetLine (info.Region.Start.Line); // If the line is already underlined if (errors.Any (em => em.LineSegment == line)) return; ErrorMarker error = new ErrorMarker (info, line); errors.Add (error); doc.AddMarker (line, error); }
internal static string FormatMessage (string msg) { StringBuilder sb = new StringBuilder (); bool wasWs = false; foreach (char ch in msg) { if (ch == ' ' || ch == '\t') { if (!wasWs) sb.Append (' '); wasWs = true; continue; } wasWs = false; sb.Append (ch); } Document doc = new Document (); doc.Text = sb.ToString (); for (int i = 1; i <= doc.LineCount; i++) { string text = doc.GetLineText (i).Trim (); int idx = text.IndexOf (':'); if (text.StartsWith ("*") && idx >= 0 && idx < text.Length - 1) { int offset = doc.GetLine (i).EndOffset; msg = text.Substring (idx + 1) + doc.GetTextAt (offset, doc.Length - offset); break; } } return msg.TrimStart (' ', '\t'); }
static void CheckStartPoint (Document doc, InsertionPoint point) { LineSegment line = doc.GetLine (point.Location.Line); if (line == null) return; if (doc.GetLineIndent (line).Length < point.Location.Column) point.ShouldInsertNewLineBefore = true; if (point.Location.Column < line.EditableLength) point.ShouldInsertNewLineAfter = true; }