public static void ShowAsSearchResults(string title, List <Reference> list) { if (list == null) { return; } List <SearchResultMatch> results = new List <SearchResultMatch>(list.Count); TextDocument document = null; ITextBuffer buffer = null; FileName fileName = null; ISyntaxHighlighter highlighter = null; foreach (Reference r in list) { var f = new FileName(r.FileName); if (document == null || !f.Equals(fileName)) { buffer = ParserService.GetParseableFileContent(r.FileName); document = new TextDocument(DocumentUtilitites.GetTextSource(buffer)); fileName = new FileName(r.FileName); highlighter = EditorControlService.Instance.CreateHighlighter(new AvalonEditDocumentAdapter(document, null), fileName); } var start = document.GetLocation(r.Offset).ToLocation(); var end = document.GetLocation(r.Offset + r.Length).ToLocation(); var builder = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter); SearchResultMatch res = new SearchResultMatch(fileName, start, end, r.Offset, r.Length, builder, highlighter.DefaultTextColor); results.Add(res); } SearchResultsPad.Instance.ShowSearchResults(title, results); SearchResultsPad.Instance.BringToFront(); }
static HighlightingColor GetDefaultHighlightTextColor(ISyntaxHighlighter highlighter) { if (highlighter != null) { return(highlighter.DefaultTextColor); } return(null); }
public AutocompleteConsole ConstructorTest( string prompt, IScanner scanner, ISyntaxHighlighter highlighter, IAutocompletionSource sources, bool useNativeApi ) { AutocompleteConsole target = new AutocompleteConsole(prompt, scanner, highlighter, sources, useNativeApi); return(target); // TODO: add assertions to method AutocompleteConsoleTest.ConstructorTest(String, IScanner, ISyntaxHighlighter, IAutocompletionSource, Boolean) }
SearchedFile SearchFile(FileName fileName) { ITextBuffer buffer = fileFinder.Create(fileName); if (buffer == null) { return(null); } ThrowIfCancellationRequested(); var source = DocumentUtilitites.GetTextSource(buffer); TextDocument document = null; ISyntaxHighlighter highlighter = null; int offset = 0; int length = source.TextLength; if (Target == SearchTarget.CurrentSelection && Selection != null) { offset = Selection.Offset; length = Selection.Length; } List <SearchResultMatch> results = new List <SearchResultMatch>(); foreach (var result in strategy.FindAll(source, offset, length)) { ThrowIfCancellationRequested(); if (document == null) { document = new TextDocument(source); var highlighting = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(fileName)); highlighter = EditorControlService.Instance.CreateHighlighter(new AvalonEditDocumentAdapter(document, null), fileName); } var start = document.GetLocation(result.Offset).ToLocation(); var end = document.GetLocation(result.Offset + result.Length).ToLocation(); var builder = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter); results.Add(new AvalonEditSearchResultMatch(fileName, start, end, result.Offset, result.Length, builder, highlighter, result)); } if (results.Count > 0) { return(new SearchedFile(fileName, results)); } else { return(null); } }
/// <param name="singleLineMode"> /// Editor uses only 1 editable line. /// </param> /// <param name="readOnly"></param> /// <param name="prompt"> /// A permanent prompt to user to input something. Works only with /// <paramref name="singleLineMode" />. /// </param> /// <param name="highlighter"> /// A code highlighter what is used in that editor. /// </param> public CliEditorSettings( bool singleLineMode = false, bool readOnly = false, string prompt = null, ISyntaxHighlighter highlighter = null ) { SingleLineMode = singleLineMode; Prompt = SingleLineMode ? prompt ?? "" : prompt != null ? throw new ArgumentException() : ""; ReadOnly = readOnly; Tabulation = new string(' ', 4); Highlighter = highlighter; }
public AutocompleteConsole( string prompt, IScanner scanner, ISyntaxHighlighter highlighter, IAutocompletionSource sources, bool useNativeApi) { _prompt = prompt; Scanner = scanner; Highlighter = highlighter; Source = sources; if (UseNativeApi = (useNativeApi && GetCurrentProcess().MainWindowHandle != IntPtr.Zero)) { _gfx = new ConsoleGraphics(); _gfx.Init(); } }
protected override ITextMarker CreateMarker(ITextMarkerService markerService) { IDocumentLine line = this.Document.GetLine(this.LineNumber); ITextMarker marker = markerService.Create(line.Offset, line.Length); ISyntaxHighlighter highlighter = this.Document.GetService(typeof(ISyntaxHighlighter)) as ISyntaxHighlighter; marker.BackgroundColor = DefaultBackground; marker.ForegroundColor = DefaultForeground; if (highlighter != null) { var color = highlighter.GetNamedColor(BreakpointMarker); if (color != null) { marker.BackgroundColor = color.Background.GetColor(null); marker.ForegroundColor = color.Foreground.GetColor(null); } } return(marker); }
protected override ITextMarker CreateMarker(ITextMarkerService markerService) { IDocumentLine line = this.Document.GetLine(startLine); ITextMarker marker = markerService.Create(line.Offset + startColumn - 1, Math.Max(endColumn - startColumn, 1)); ISyntaxHighlighter highlighter = this.Document.GetService(typeof(ISyntaxHighlighter)) as ISyntaxHighlighter; marker.BackgroundColor = DefaultBackground; marker.ForegroundColor = DefaultForeground; if (highlighter != null) { var color = highlighter.GetNamedColor(Name); if (color != null) { marker.BackgroundColor = color.Background.GetColor(null); marker.ForegroundColor = color.Foreground.GetColor(null); } } return(marker); }
/// <summary> /// CSV File to display /// </summary> public void OpenFile(bool json, string qualifier, string delimiter, string escape, int codePage, int skipLines, string comment) { if (!FileSystemUtils.FileExists(m_FullPath)) { textBox.Text = $"\nThe file '{m_FullPath}' does not exist."; } else { try { if (json) { m_HighLighter = new SyntaxHighlighterJson(textBox); textBox.ContextMenuStrip = contextMenuJson; } else { m_HighLighter = new SyntaxHighlighterDelimitedText(textBox, qualifier, delimiter, escape, comment); } m_Stream = new ImprovedStream(new SourceAccess(m_FullPath)); m_SkipLines = !json ? skipLines : 0; m_CodePage = codePage; OriginalStream(); } catch (Exception ex) { m_HighLighter = null; textBox.Text = $"Issue opening the file {m_FullPath} for display:\n\n\n{ex.Message}"; } } }
public static HighlightedInlineBuilder CreateInlineBuilder(Location startPosition, Location endPosition, TextDocument document, ISyntaxHighlighter highlighter) { if (startPosition.Line >= 1 && startPosition.Line <= document.LineCount) { HighlightedInlineBuilder inlineBuilder; if (highlighter != null) { inlineBuilder = highlighter.BuildInlines(startPosition.Line); } else { inlineBuilder = new HighlightedInlineBuilder(document.GetText(document.GetLineByNumber(startPosition.Line))); } // now highlight the match in bold if (startPosition.Column >= 1) { if (endPosition.Line == startPosition.Line && endPosition.Column > startPosition.Column) { // subtract one from the column to get the offset inside the line's text int startOffset = startPosition.Column - 1; int endOffset = Math.Min(inlineBuilder.Text.Length, endPosition.Column - 1); inlineBuilder.SetFontWeight(startOffset, endOffset - startOffset, FontWeights.Bold); } } return(inlineBuilder); } return(null); }
public AvalonEditSearchResultMatch(FileName fileName, Location startLocation, Location endLocation, int offset, int length, HighlightedInlineBuilder builder, ISyntaxHighlighter highlighter, ICSharpCode.AvalonEdit.Search.ISearchResult match) : base(fileName, startLocation, endLocation, offset, length, builder, GetDefaultHighlightTextColor(highlighter)) { this.match = match; }
static HighlightingColor GetDefaultHighlightTextColor(ISyntaxHighlighter highlighter) { if (highlighter != null) { return highlighter.DefaultTextColor; } return null; }
void FormatLineInternal(ITextEditor textArea, int lineNr, int cursorOffset, char ch) { IDocumentLine curLine = textArea.Document.GetLine(lineNr); IDocumentLine lineAbove = lineNr > 1 ? textArea.Document.GetLine(lineNr - 1) : null; string terminator = DocumentUtilitites.GetLineTerminator(textArea.Document, lineNr); string curLineText; //// local string for curLine segment if (ch == '-') { curLineText = curLine.Text; string lineAboveText = lineAbove == null ? "" : lineAbove.Text; if (curLineText != null && curLineText.EndsWith("---") && (lineAboveText == null || !lineAboveText.Trim().StartsWith("---"))) { string indentation = DocumentUtilitites.GetWhitespaceAfter(textArea.Document, curLine.Offset); StringBuilder sb = new StringBuilder(); sb.Append(" <summary>"); sb.Append(terminator); sb.Append(indentation); sb.Append("--- "); sb.Append(terminator); sb.Append(indentation); sb.Append("--- </summary>"); //sb.Append(terminator); //sb.Append(indentation); //sb.Append("--- <returns></returns>"); textArea.Document.Insert(cursorOffset, sb.ToString()); textArea.Caret.Offset = cursorOffset + indentation.Length + "--- ".Length + " <summary>".Length + terminator.Length; } else if (curLineText != null && curLineText.Trim() == "-" && (lineAboveText != null && lineAboveText.Trim().StartsWith("---"))) { StringBuilder sb = new StringBuilder(); sb.Append("-- "); //sb.Append(terminator); //sb.Append(indentation); //sb.Append("--- <returns></returns>"); textArea.Document.Insert(cursorOffset, sb.ToString()); textArea.Caret.Offset = cursorOffset + "-- ".Length; } return; } if (ch != '\n' && ch != '>') { if (IsInsideStringOrComment(textArea, curLine, cursorOffset)) { return; } } if (IsInsideStringOrComment(textArea, curLine, cursorOffset) == false && textArea.Caret.Offset == curLine.EndOffset && // end of line, not editing something inside the line (curLine.Text.TrimEnd().EndsWith("then") || curLine.Text.TrimEnd().EndsWith("do"))) { string indentation = DocumentUtilitites.GetWhitespaceAfter(textArea.Document, curLine.Offset); StringBuilder sb = new StringBuilder(); sb.Append(terminator); sb.Append(indentation + textArea.Options.IndentationString); sb.Append(terminator); sb.Append(indentation); sb.Append("end"); textArea.Document.Insert(cursorOffset, sb.ToString()); textArea.Caret.Offset = cursorOffset + terminator.Length // end of line + indentation.Length // indentation + textArea.Options.IndentationString.Length ; } if (IsInsideStringOrComment(textArea, curLine, cursorOffset) == false && textArea.Caret.Offset == curLine.EndOffset && // end of line, not editing something inside the line curLine.Text.TrimStart().StartsWith("repeat")) { string indentation = DocumentUtilitites.GetWhitespaceAfter(textArea.Document, curLine.Offset); StringBuilder sb = new StringBuilder(); sb.Append(terminator); sb.Append(indentation + textArea.Options.IndentationString); sb.Append(terminator); sb.Append(indentation); sb.Append("until "); textArea.Document.Insert(cursorOffset, sb.ToString()); textArea.Caret.Offset = cursorOffset + indentation.Length + terminator.Length // line 1 + indentation.Length + textArea.Options.IndentationString.Length + terminator.Length // line 2 + "until ".Length ; } switch (ch) { case '>': if (IsInsideDocumentationComment(textArea, curLine, cursorOffset)) { curLineText = curLine.Text; int column = cursorOffset - curLine.Offset; int index = Math.Min(column - 1, curLineText.Length - 1); while (index >= 0 && curLineText[index] != '<') { --index; if (curLineText[index] == '/') { return; // the tag was an end tag or already } } if (index > 0) { StringBuilder commentBuilder = new StringBuilder(""); for (int i = index; i < curLineText.Length && i < column && !Char.IsWhiteSpace(curLineText[i]); ++i) { commentBuilder.Append(curLineText[i]); } string tag = commentBuilder.ToString().Trim(); if (!tag.EndsWith(">")) { tag += ">"; } if (!tag.StartsWith("/")) { textArea.Document.Insert(cursorOffset, "</" + tag.Substring(1), AnchorMovementType.BeforeInsertion); } } } break; case ')': if (curLine.Text.TrimStart().StartsWith("function")) { string indentation = DocumentUtilitites.GetWhitespaceAfter(textArea.Document, curLine.Offset); StringBuilder sb = new StringBuilder(); sb.Append(terminator); sb.Append(indentation + textArea.Options.IndentationString); sb.Append(terminator); sb.Append(indentation); sb.Append("end"); textArea.Document.Insert(cursorOffset, sb.ToString()); textArea.Caret.Offset = cursorOffset + terminator.Length // end of line + indentation.Length // indentation + textArea.Options.IndentationString.Length ; } else { IndentLine(textArea, curLine); } break; case ':': case ']': case '}': case '{': IndentLine(textArea, curLine); break; case '\n': string lineAboveText = lineAbove == null ? "" : lineAbove.Text; //// curLine might have some text which should be added to indentation curLineText = curLine.Text; ISyntaxHighlighter highlighter = textArea.GetService(typeof(ISyntaxHighlighter)) as ISyntaxHighlighter; bool isInMultilineComment = false; bool isInMultilineString = false; if (highlighter != null && lineAbove != null) { var spanStack = highlighter.GetSpanColorNamesFromLineStart(lineNr); isInMultilineComment = spanStack.Contains(SyntaxHighligherKnownSpanNames.Comment); isInMultilineString = spanStack.Contains(SyntaxHighligherKnownSpanNames.String); } bool isInNormalCode = !(isInMultilineComment || isInMultilineString); if (lineAbove != null && isInMultilineComment) { string lineAboveTextTrimmed = lineAboveText.TrimStart(); if (lineAboveTextTrimmed.StartsWith("--[[", StringComparison.Ordinal)) { textArea.Document.Insert(cursorOffset, " - "); return; } if (lineAboveTextTrimmed.StartsWith("-", StringComparison.Ordinal)) { textArea.Document.Insert(cursorOffset, "- "); return; } } if (lineAbove != null && isInNormalCode) { IDocumentLine nextLine = lineNr + 1 <= textArea.Document.TotalNumberOfLines ? textArea.Document.GetLine(lineNr + 1) : null; string nextLineText = (nextLine != null) ? nextLine.Text : ""; int indexAbove = lineAboveText.IndexOf("---"); int indexNext = nextLineText.IndexOf("---"); if (indexAbove > 0 && (indexNext != -1 || indexAbove + 4 < lineAbove.Length)) { textArea.Document.Insert(cursorOffset, "--- "); return; } if (IsInNonVerbatimString(lineAboveText, curLineText)) { textArea.Document.Insert(cursorOffset, "\""); textArea.Document.Insert(lineAbove.Offset + lineAbove.Length, "\" +"); } } return; } }
private void UpdateHighlight() { m_HighLighter = new SyntaxHighlighterDelimitedText(textBox, m_TextBoxQuote.Text, textBoxDelimiter.Text, fileSetting.FileFormat.EscapeCharacter, textBoxComment.Text); }
public static HighlightedInlineBuilder CreateInlineBuilder(Location startPosition, Location endPosition, TextDocument document, ISyntaxHighlighter highlighter) { if (startPosition.Line >= 1 && startPosition.Line <= document.LineCount) { HighlightedInlineBuilder inlineBuilder; if (highlighter != null) { inlineBuilder = highlighter.BuildInlines(startPosition.Line); } else { inlineBuilder = new HighlightedInlineBuilder(document.GetText(document.GetLineByNumber(startPosition.Line))); } // now highlight the match in bold if (startPosition.Column >= 1) { if (endPosition.Line == startPosition.Line && endPosition.Column > startPosition.Column) { // subtract one from the column to get the offset inside the line's text int startOffset = startPosition.Column - 1; int endOffset = Math.Min(inlineBuilder.Text.Length, endPosition.Column - 1); inlineBuilder.SetFontWeight(startOffset, endOffset - startOffset, FontWeights.Bold); } } return inlineBuilder; } return null; }
public static bool IsLineStartInsideString(this ISyntaxHighlighter highligher, int lineNumber) { return(highligher.GetSpanColorNamesFromLineStart(lineNumber).Contains(String)); }
void FormatLineInternal(ITextEditor textArea, int lineNr, int cursorOffset, char ch) { IDocumentLine curLine = textArea.Document.GetLine(lineNr); IDocumentLine lineAbove = lineNr > 1 ? textArea.Document.GetLine(lineNr - 1) : null; string terminator = DocumentUtilitites.GetLineTerminator(textArea.Document, lineNr); string curLineText; //// local string for curLine segment if (ch == '/') { curLineText = curLine.Text; string lineAboveText = lineAbove == null ? "" : lineAbove.Text; if (curLineText != null && curLineText.EndsWith("///") && (lineAboveText == null || !lineAboveText.Trim().StartsWith("///"))) { string indentation = DocumentUtilitites.GetWhitespaceAfter(textArea.Document, curLine.Offset); object member = GetMemberAfter(textArea, lineNr); if (member != null) { StringBuilder sb = new StringBuilder(); sb.Append(" <summary>"); sb.Append(terminator); sb.Append(indentation); sb.Append("/// "); sb.Append(terminator); sb.Append(indentation); sb.Append("/// </summary>"); if (member is IMethod) { IMethod method = (IMethod)member; if (method.Parameters != null && method.Parameters.Count > 0) { for (int i = 0; i < method.Parameters.Count; ++i) { sb.Append(terminator); sb.Append(indentation); sb.Append("/// <param name=\""); sb.Append(method.Parameters[i].Name); sb.Append("\"></param>"); } } if (method.ReturnType != null && !method.IsConstructor && method.ReturnType.FullyQualifiedName != "System.Void") { sb.Append(terminator); sb.Append(indentation); sb.Append("/// <returns></returns>"); } } textArea.Document.Insert(cursorOffset, sb.ToString()); textArea.Caret.Offset = cursorOffset + indentation.Length + "/// ".Length + " <summary>".Length + terminator.Length; } } return; } if (ch != '\n' && ch != '>') { if (IsInsideStringOrComment(textArea, curLine, cursorOffset)) { return; } } switch (ch) { case '>': if (IsInsideDocumentationComment(textArea, curLine, cursorOffset)) { curLineText = curLine.Text; int column = cursorOffset - curLine.Offset; int index = Math.Min(column - 1, curLineText.Length - 1); while (index >= 0 && curLineText[index] != '<') { --index; if (curLineText[index] == '/') { return; // the tag was an end tag or already } } if (index > 0) { StringBuilder commentBuilder = new StringBuilder(""); for (int i = index; i < curLineText.Length && i < column && !Char.IsWhiteSpace(curLineText[i]); ++i) { commentBuilder.Append(curLineText[i]); } string tag = commentBuilder.ToString().Trim(); if (!tag.EndsWith(">")) { tag += ">"; } if (!tag.StartsWith("/")) { textArea.Document.Insert(cursorOffset, "</" + tag.Substring(1), AnchorMovementType.BeforeInsertion); } } } break; case ':': case ')': case ']': case '}': case '{': //if (textArea.Document.TextEditorProperties.IndentStyle == IndentStyle.Smart) { IndentLine(textArea, curLine); //} break; case '\n': string lineAboveText = lineAbove == null ? "" : lineAbove.Text; //// curLine might have some text which should be added to indentation curLineText = curLine.Text; if (lineAbove != null && lineAbove.Text.Trim().StartsWith("#region") && NeedEndregion(textArea.Document)) { textArea.Document.Insert(cursorOffset, "#endregion"); return; } ISyntaxHighlighter highlighter = textArea.GetService(typeof(ISyntaxHighlighter)) as ISyntaxHighlighter; bool isInMultilineComment = false; bool isInMultilineString = false; if (highlighter != null && lineAbove != null) { var spanStack = highlighter.GetSpanColorNamesFromLineStart(lineNr); isInMultilineComment = spanStack.Contains(SyntaxHighligherKnownSpanNames.Comment); isInMultilineString = spanStack.Contains(SyntaxHighligherKnownSpanNames.String); } bool isInNormalCode = !(isInMultilineComment || isInMultilineString); if (lineAbove != null && isInMultilineComment) { string lineAboveTextTrimmed = lineAboveText.TrimStart(); if (lineAboveTextTrimmed.StartsWith("/*", StringComparison.Ordinal)) { textArea.Document.Insert(cursorOffset, " * "); return; } if (lineAboveTextTrimmed.StartsWith("*", StringComparison.Ordinal)) { textArea.Document.Insert(cursorOffset, "* "); return; } } if (lineAbove != null && isInNormalCode) { IDocumentLine nextLine = lineNr + 1 <= textArea.Document.TotalNumberOfLines ? textArea.Document.GetLine(lineNr + 1) : null; string nextLineText = (nextLine != null) ? nextLine.Text : ""; int indexAbove = lineAboveText.IndexOf("///"); int indexNext = nextLineText.IndexOf("///"); if (indexAbove > 0 && (indexNext != -1 || indexAbove + 4 < lineAbove.Length)) { textArea.Document.Insert(cursorOffset, "/// "); return; } if (IsInNonVerbatimString(lineAboveText, curLineText)) { textArea.Document.Insert(cursorOffset, "\""); textArea.Document.Insert(lineAbove.Offset + lineAbove.Length, "\" +"); } } if (textArea.Options.AutoInsertBlockEnd && lineAbove != null && isInNormalCode) { string oldLineText = lineAbove.Text; if (oldLineText.EndsWith("{")) { if (NeedCurlyBracket(textArea.Document.Text)) { int insertionPoint = curLine.Offset + curLine.Length; textArea.Document.Insert(insertionPoint, terminator + "}"); IndentLine(textArea, textArea.Document.GetLine(lineNr + 1)); textArea.Caret.Offset = insertionPoint; } } } return; } }