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 SetDiffCellData(Gtk.TreeViewColumn tree_column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) { try { CellRendererDiff cellRendererDiff = (CellRendererDiff)cell; Change change = store.GetValue(iter, objColumn) as Change; cellRendererDiff.Visible = !(bool)store.GetValue(iter, statusVisibleColumn); if (change == null || !cellRendererDiff.Visible) { cellRendererDiff.InitCell(treeviewPreview, false, "", ""); return; } TextReplaceChange replaceChange = change as TextReplaceChange; if (replaceChange == null) { return; } Mono.TextEditor.Document doc = new Mono.TextEditor.Document(); doc.Text = System.IO.File.ReadAllText(replaceChange.FileName); List <string> before = new List <string> (); foreach (var line in doc.Lines) { before.Add(doc.GetTextAt(line.Offset, line.EditableLength)); } ((Mono.TextEditor.IBuffer)doc).Replace(replaceChange.Offset, replaceChange.RemovedChars, replaceChange.InsertedText); List <string> after = new List <string> (); foreach (var line in doc.Lines) { after.Add(doc.GetTextAt(line.Offset, line.EditableLength)); } Diff diff = new Diff(before.ToArray(), after.ToArray(), true, true); System.IO.StringWriter w = new System.IO.StringWriter(); UnifiedDiff.WriteUnifiedDiff(diff, w, replaceChange.FileName, replaceChange.FileName, 2); cellRendererDiff.InitCell(treeviewPreview, true, w.ToString().Trim(), replaceChange.FileName); } catch (Exception e) { Console.WriteLine(e); } }
static int StartsWithListMember (Document document, List<string> list, int offset) { for (int i = 0; i < list.Count; i++) { string item = list[i]; if (offset + item.Length < document.Length) { if (document.GetTextAt (offset, item.Length) == item) return i; } } return -1; }
internal static void EvaluateMethodLOC(MethodProperties prop, List <LineSegment> text, Mono.TextEditor.Document doc) { ulong totalLines = 0, totalRealLines = 0, totalCommentedLines = 0; int realLines = 0; bool isSingleLineComment = false; bool isMultipleLineComment = false; int startIndex = prop.StartLine; int endIndex = prop.EndLine; for (int i = startIndex; i < endIndex; i++) { string lineText = ""; try{ lineText = doc.GetTextAt(text[i]).Trim(); } catch (Exception e) { continue; } if (isMultipleLineComment) { totalCommentedLines++; if (lineText.EndsWith("*/")) { isMultipleLineComment = false; } continue; } if (lineText.StartsWith("/*")) { isMultipleLineComment = true; totalCommentedLines++; continue; } isSingleLineComment = lineText.StartsWith("//"); if (isSingleLineComment) { totalCommentedLines++; } if (lineText.Length > 0 && !isSingleLineComment) { realLines++; } } totalLines += (ulong)(endIndex - startIndex + 1); totalRealLines += (ulong)realLines; ((MethodProperties)prop).LOCComments = totalCommentedLines; ((MethodProperties)prop).LOCReal = totalRealLines + 1; }
public override void Analyze (Document doc, LineSegment line, Chunk startChunk, int startOffset, int endOffset) { if (endOffset <= startOffset || startOffset >= doc.Length) return; string text = doc.GetTextAt (startOffset, endOffset - startOffset); int startColumn = startOffset - line.Offset; line.RemoveMarker (typeof(UrlMarker)); foreach (System.Text.RegularExpressions.Match m in urlRegex.Matches (text)) { line.AddMarker (new UrlMarker (line, m.Value, UrlType.Url, syntax, startColumn + m.Index, startColumn + m.Index + m.Length)); } foreach (System.Text.RegularExpressions.Match m in mailRegex.Matches (text)) { line.AddMarker (new UrlMarker (line, m.Value, UrlType.Email, syntax, startColumn + m.Index, startColumn + m.Index + m.Length)); } }
static string AddIndent(string text, string indent) { Mono.TextEditor.Document doc = new Mono.TextEditor.Document(); doc.Text = text; StringBuilder result = new StringBuilder(); foreach (LineSegment line in doc.Lines) { if (result.Length > 0) { result.Append(indent); } result.Append(doc.GetTextAt(line)); } return(result.ToString()); }
public override void Analyze (Document doc, LineSegment line, Chunk startChunk, int startOffset, int endOffset) { if (endOffset <= startOffset || startOffset >= doc.Length || inUpdate) return; inUpdate = true; try { string text = doc.GetTextAt (startOffset, endOffset - startOffset); int startColumn = startOffset - line.Offset; var markers = new List <UrlMarker> (line.Markers.Where (m => m is UrlMarker).Cast<UrlMarker> ()); markers.ForEach (m => doc.RemoveMarker (m, false)); foreach (System.Text.RegularExpressions.Match m in urlRegex.Matches (text)) { doc.AddMarker (line, new UrlMarker (doc, line, m.Value, UrlType.Url, syntax, startColumn + m.Index, startColumn + m.Index + m.Length), false); } foreach (System.Text.RegularExpressions.Match m in mailRegex.Matches (text)) { doc.AddMarker (line, new UrlMarker (doc, line, m.Value, UrlType.Email, syntax, startColumn + m.Index, startColumn + m.Index + m.Length), false); } } finally { inUpdate = false; } }
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 string AddIndent (string text, string indent) { Document doc = new Document (); doc.Text = text; StringBuilder result = new StringBuilder (); foreach (LineSegment line in doc.Lines) { if (result.Length > 0) result.Append (indent); result.Append (doc.GetTextAt (line)); } return result.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 (); }
static MonoDevelop.Ide.FindInFiles.SearchResult GetJumpTypePartSearchResult (IType part) { var provider = new MonoDevelop.Ide.FindInFiles.FileProvider (part.CompilationUnit.FileName); var doc = new Mono.TextEditor.Document (); doc.Text = provider.ReadString (); int position = doc.LocationToOffset (part.Location.Line, part.Location.Column); while (position + part.Name.Length < doc.Length) { if (doc.GetTextAt (position, part.Name.Length) == part.Name) break; position++; } return new MonoDevelop.Ide.FindInFiles.SearchResult (provider, position, part.Name.Length); }
public virtual string GetText (Document doc) { return doc.GetTextAt (this); }
void SetDiffCellData (Gtk.TreeViewColumn tree_column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) { try { CellRendererDiff cellRendererDiff = (CellRendererDiff)cell; Change change = store.GetValue (iter, objColumn) as Change; cellRendererDiff.Visible = !(bool)store.GetValue (iter, statusVisibleColumn); if (change == null || !cellRendererDiff.Visible) { cellRendererDiff.InitCell (treeviewPreview, false, "", ""); return; } TextReplaceChange replaceChange = change as TextReplaceChange; if (replaceChange == null) return; Mono.TextEditor.Document doc = new Mono.TextEditor.Document (); doc.Text = System.IO.File.ReadAllText (replaceChange.FileName); List<string> before = new List<string> (); foreach (var line in doc.Lines) { before.Add (doc.GetTextAt (line.Offset, line.EditableLength)); } ((Mono.TextEditor.IBuffer)doc).Replace (replaceChange.Offset, replaceChange.RemovedChars, replaceChange.InsertedText); List<string> after = new List<string> (); foreach (var line in doc.Lines) { after.Add (doc.GetTextAt (line.Offset, line.EditableLength)); } Diff diff = new Diff (before.ToArray (), after.ToArray (), true, true); System.IO.StringWriter w = new System.IO.StringWriter(); UnifiedDiff.WriteUnifiedDiff (diff, w, replaceChange.FileName, replaceChange.FileName, 2); cellRendererDiff.InitCell (treeviewPreview, true, w.ToString ().Trim (), replaceChange.FileName); } catch (Exception e) { Console.WriteLine (e); } }
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(); }
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); } }