public static void SpellCheck(IDocument document, ITokenNode token, ISpellChecker spellChecker, ISolution solution, DefaultHighlightingConsumer consumer, IContextBoundSettingsStore settingsStore, StringSettings settings) { if (spellChecker == null) { return; } string buffer = unescape(token.GetText()); ILexer wordLexer = new WordLexer(buffer); wordLexer.Start(); while (wordLexer.TokenType != null) { string tokenText = wordLexer.GetCurrTokenText(); if (SpellCheckUtil.ShouldSpellCheck(tokenText, settings.CompiledWordsToIgnore) && !spellChecker.TestWord(tokenText, true)) { IClassMemberDeclaration containingElement = token.GetContainingNode <IClassMemberDeclaration>(false); if (containingElement == null || !IdentifierResolver.IsIdentifier(containingElement, solution, tokenText)) { CamelHumpLexer camelHumpLexer = new CamelHumpLexer(buffer, wordLexer.TokenStart, wordLexer.TokenEnd); foreach (LexerToken humpToken in camelHumpLexer) { if (SpellCheckUtil.ShouldSpellCheck(humpToken.Value, settings.CompiledWordsToIgnore) && !spellChecker.TestWord(humpToken.Value, true)) { //int start = token.GetTreeStartOffset().Offset + wordLexer.TokenStart; //int end = start + tokenText.Length; //TextRange range = new TextRange(start, end); //DocumentRange documentRange = new DocumentRange(document, range); DocumentRange documentRange = token.GetContainingFile().TranslateRangeForHighlighting(token.GetTreeTextRange()); documentRange = documentRange.ExtendLeft(-wordLexer.TokenStart); documentRange = documentRange.ExtendRight(-1 * (documentRange.GetText().Length - tokenText.Length)); TextRange textRange = new TextRange(humpToken.Start - wordLexer.TokenStart, humpToken.End - wordLexer.TokenStart); //string word = document.GetText(range); string word = documentRange.GetText(); consumer.AddHighlighting( new StringSpellCheckHighlighting( word, documentRange, humpToken.Value, textRange, solution, spellChecker, settingsStore), documentRange); break; } } } } wordLexer.Advance(); } }
/// <summary> /// Gets a trimmed DocumentRange for the DocumentRange provided. /// </summary> /// <param name="documentRange"> /// The DocumentRange to use. /// </param> /// <returns> /// A DocumentRange with whitespace removed from the left and right. /// </returns> public static DocumentRange TrimWhitespaceFromDocumentRange(DocumentRange documentRange) { using (ReadLockCookie.Create()) { JB::JetBrains.Util.TextRange textRange = documentRange.TextRange; string text = documentRange.GetText(); int newLeft = CountOfWhitespaceAtLeft(text); int newRight = CountOfWhitespaceAtRight(text); JB::JetBrains.Util.TextRange a = textRange.TrimLeft(newLeft); return new DocumentRange(documentRange.Document, a.TrimRight(newRight)); } }
private static string CalculateCLikeCommentPresentation(DocumentRange range, int countNewLines, string prefix = "/*", string postfix = "*/") { var text = range.GetText(); var newLineIndex = -1; var firstLine = string.Empty; while (true) { var prevIndex = newLineIndex; if (prevIndex + 1 >= text.Length) { break; } newLineIndex = text.IndexOfAny(StringUtil.NEW_LINE_CHARACTERS, prevIndex + 1); if (prevIndex > 0 && newLineIndex == -1) { newLineIndex = text.Length; } else if (newLineIndex >= text.Length || newLineIndex == -1) { break; } var line = text.Substring(prevIndex + 1, newLineIndex - prevIndex - 1); var lineLength = line.Length; int shiftStart; for (shiftStart = 0; shiftStart < lineLength; shiftStart++) { var character = line[shiftStart]; if (char.IsWhiteSpace(character) || character == '/' || character == '*') { continue; } break; } if (shiftStart == lineLength) { continue; } var shiftEnd = 0; for (var i = lineLength - 1; i >= 0; i--) { var character = line[i]; if (char.IsWhiteSpace(character) || character == '/' || character == '*') { shiftEnd++; continue; } break; } var start = shiftStart; if (start >= lineLength || lineLength - shiftStart - shiftEnd < 0) { continue; } var sub = line.Substring(shiftStart, lineLength - shiftStart - shiftEnd).Trim(); if (sub.IsEmpty()) { continue; } if (sub.Length > MaxRemainingCommentTextLength) { sub = sub.Substring(0, MaxRemainingCommentTextLength); } firstLine = " " + sub; break; } return(prefix + firstLine + " ... " + postfix); }