Java Code Examples for com.intellij.util.text.CharArrayUtil#shiftForward()
The following examples show how to use
com.intellij.util.text.CharArrayUtil#shiftForward() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilteredIndentsHighlightingPass.java From flutter-intellij with BSD 3-Clause "New" or "Revised" License | 6 votes |
private int findCodeConstructStartLine(int startLine) { DocumentEx document = myEditor.getDocument(); CharSequence text = document.getImmutableCharSequence(); int lineStartOffset = document.getLineStartOffset(startLine); int firstNonWsOffset = CharArrayUtil.shiftForward(text, lineStartOffset, " \t"); FileType type = PsiUtilBase.getPsiFileAtOffset(myFile, firstNonWsOffset).getFileType(); Language language = PsiUtilCore.getLanguageAtOffset(myFile, firstNonWsOffset); BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(type, language); HighlighterIterator iterator = myEditor.getHighlighter().createIterator(firstNonWsOffset); if (braceMatcher.isLBraceToken(iterator, text, type)) { int codeConstructStart = braceMatcher.getCodeConstructStart(myFile, firstNonWsOffset); return document.getLineNumber(codeConstructStart); } else { return startLine; } }
Example 2
Source File: CommentByBlockCommentHandler.java From consulo with Apache License 2.0 | 6 votes |
private TextRange expandRange(int delOffset1, int delOffset2) { CharSequence chars = myDocument.getCharsSequence(); int offset1 = CharArrayUtil.shiftBackward(chars, delOffset1 - 1, " \t"); if (offset1 < 0 || chars.charAt(offset1) == '\n' || chars.charAt(offset1) == '\r') { int offset2 = CharArrayUtil.shiftForward(chars, delOffset2, " \t"); if (offset2 == myDocument.getTextLength() || chars.charAt(offset2) == '\r' || chars.charAt(offset2) == '\n') { delOffset1 = offset1 + 1; if (offset2 < myDocument.getTextLength()) { delOffset2 = offset2 + 1; if (chars.charAt(offset2) == '\r' && offset2 + 1 < myDocument.getTextLength() && chars.charAt(offset2 + 1) == '\n') { delOffset2++; } } } } return new TextRange(delOffset1, delOffset2); }
Example 3
Source File: FilteredIndentsHighlightingPass.java From flutter-intellij with BSD 3-Clause "New" or "Revised" License | 6 votes |
private int findCodeConstructStartLine(int startLine) { DocumentEx document = myEditor.getDocument(); CharSequence text = document.getImmutableCharSequence(); int lineStartOffset = document.getLineStartOffset(startLine); int firstNonWsOffset = CharArrayUtil.shiftForward(text, lineStartOffset, " \t"); FileType type = PsiUtilBase.getPsiFileAtOffset(myFile, firstNonWsOffset).getFileType(); Language language = PsiUtilCore.getLanguageAtOffset(myFile, firstNonWsOffset); BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(type, language); HighlighterIterator iterator = myEditor.getHighlighter().createIterator(firstNonWsOffset); if (braceMatcher.isLBraceToken(iterator, text, type)) { int codeConstructStart = braceMatcher.getCodeConstructStart(myFile, firstNonWsOffset); return document.getLineNumber(codeConstructStart); } else { return startLine; } }
Example 4
Source File: PasteHandler.java From consulo with Apache License 2.0 | 6 votes |
@SuppressWarnings("ForLoopThatDoesntUseLoopVariable") private static void indentPlainTextBlock(final Document document, final int startOffset, final int endOffset, final int indentLevel) { CharSequence chars = document.getCharsSequence(); int spaceEnd = CharArrayUtil.shiftForward(chars, startOffset, " \t"); int line = document.getLineNumber(startOffset); if (spaceEnd > endOffset || indentLevel <= 0 || line >= document.getLineCount() - 1 || chars.charAt(spaceEnd) == '\n') { return; } int linesToAdjustIndent = 0; for (int i = line + 1; i < document.getLineCount(); i++) { if (document.getLineStartOffset(i) >= endOffset) { break; } linesToAdjustIndent++; } String indentString = StringUtil.repeatSymbol(' ', indentLevel); for (; linesToAdjustIndent > 0; linesToAdjustIndent--) { int lineStartOffset = document.getLineStartOffset(++line); document.insertString(lineStartOffset, indentString); } }
Example 5
Source File: CommentUtil.java From consulo with Apache License 2.0 | 6 votes |
public static IndentData getMinLineIndent(Document document, int line1, int line2, @Nonnull PsiFile file) { CharSequence chars = document.getCharsSequence(); IndentData minIndent = null; for (int line = line1; line <= line2; line++) { int lineStart = document.getLineStartOffset(line); int textStart = CharArrayUtil.shiftForward(chars, lineStart, " \t"); if (textStart >= document.getTextLength()) { textStart = document.getTextLength(); } else { char c = chars.charAt(textStart); if (c == '\n' || c == '\r') continue; // empty line } IndentData indent = IndentData.createFrom(chars, lineStart, textStart, CodeStyle.getIndentOptions(file).TAB_SIZE); minIndent = IndentData.min(minIndent, indent); } if (minIndent == null && line1 == line2 && line1 < document.getLineCount() - 1) { return getMinLineIndent(document, line1 + 1, line1 + 1, file); } return minIndent; }
Example 6
Source File: NamedElementDuplicateHandler.java From consulo with Apache License 2.0 | 6 votes |
@Nullable private static PsiElement findNameIdentifier(Editor editor, PsiFile file, TextRange toDuplicate) { int nonWs = CharArrayUtil.shiftForward(editor.getDocument().getCharsSequence(), toDuplicate.getStartOffset(), "\n\t "); PsiElement psi = file.findElementAt(nonWs); PsiElement named = null; while (psi != null) { TextRange range = psi.getTextRange(); if (range == null || psi instanceof PsiFile || !toDuplicate.contains(psi.getTextRange())) { break; } if (psi instanceof PsiNameIdentifierOwner) { named = ((PsiNameIdentifierOwner)psi).getNameIdentifier(); } psi = psi.getParent(); } return named; }
Example 7
Source File: ParameterInfoController.java From consulo with Apache License 2.0 | 6 votes |
private int adjustOffsetToInlay(int offset) { CharSequence text = myEditor.getDocument().getImmutableCharSequence(); int hostWhitespaceStart = CharArrayUtil.shiftBackward(text, offset, WHITESPACE) + 1; int hostWhitespaceEnd = CharArrayUtil.shiftForward(text, offset, WHITESPACE); Editor hostEditor = myEditor; if (myEditor instanceof EditorWindow) { hostEditor = ((EditorWindow)myEditor).getDelegate(); hostWhitespaceStart = ((EditorWindow)myEditor).getDocument().injectedToHost(hostWhitespaceStart); hostWhitespaceEnd = ((EditorWindow)myEditor).getDocument().injectedToHost(hostWhitespaceEnd); } List<Inlay> inlays = ParameterHintsPresentationManager.getInstance().getParameterHintsInRange(hostEditor, hostWhitespaceStart, hostWhitespaceEnd); for (Inlay inlay : inlays) { int inlayOffset = inlay.getOffset(); if (myEditor instanceof EditorWindow) { if (((EditorWindow)myEditor).getDocument().getHostRange(inlayOffset) == null) continue; inlayOffset = ((EditorWindow)myEditor).getDocument().hostToInjected(inlayOffset); } return inlayOffset; } return offset; }
Example 8
Source File: IndentCalculator.java From consulo with Apache License 2.0 | 6 votes |
@Nonnull protected String getBaseIndent(@Nonnull SemanticEditorPosition currPosition) { CharSequence docChars = myEditor.getDocument().getCharsSequence(); int offset = currPosition.getStartOffset(); if (offset > 0) { int indentLineOffset = myBaseLineOffsetCalculator.getOffsetInBaseIndentLine(currPosition); if (indentLineOffset > 0) { int indentStart = CharArrayUtil.shiftBackwardUntil(docChars, indentLineOffset, "\n") + 1; if (indentStart >= 0) { int indentEnd = CharArrayUtil.shiftForward(docChars, indentStart, " \t"); if (indentEnd > indentStart) { return docChars.subSequence(indentStart, indentEnd).toString(); } } } } return ""; }
Example 9
Source File: OclCommenter.java From reasonml-idea-plugin with MIT License | 6 votes |
@NotNull private TextRange expandRange(@NotNull CharSequence chars, int delOffset1, int delOffset2) { int offset1 = CharArrayUtil.shiftBackward(chars, delOffset1 - 1, " \t"); if (offset1 < 0 || chars.charAt(offset1) == '\n' || chars.charAt(offset1) == '\r') { int offset2 = CharArrayUtil.shiftForward(chars, delOffset2, " \t"); if (offset2 == chars.length() || chars.charAt(offset2) == '\r' || chars.charAt(offset2) == '\n') { delOffset1 = (offset1 < 0) ? offset1 + 1 : offset1; if (offset2 < chars.length()) { delOffset2 = offset2 + 1; if (chars.charAt(offset2) == '\r' && offset2 + 1 < chars.length() && chars.charAt(offset2 + 1) == '\n') { delOffset2++; } } } } return new TextRange(delOffset1, delOffset2); }
Example 10
Source File: ExtendWordSelectionHandlerBase.java From consulo with Apache License 2.0 | 5 votes |
public static List<TextRange> expandToWholeLinesWithBlanks(CharSequence text, TextRange range) { List<TextRange> result = ContainerUtil.newArrayList(); result.addAll(expandToWholeLine(text, range, true)); TextRange last = result.isEmpty() ? range : result.get(result.size() - 1); int start = last.getStartOffset(); int end = last.getEndOffset(); while (true) { int blankLineEnd = CharArrayUtil.shiftForward(text, end, " \t"); if (blankLineEnd >= text.length() || text.charAt(blankLineEnd) != '\n') { break; } end = blankLineEnd + 1; } if (end == last.getEndOffset()) { while (start > 0 && text.charAt(start - 1) == '\n') { int blankLineStart = CharArrayUtil.shiftBackward(text, start - 2, " \t"); if (blankLineStart <= 0 || text.charAt(blankLineStart) != '\n') { break; } start = blankLineStart + 1; } } if (start != last.getStartOffset() || end != last.getEndOffset()) { result.add(new TextRange(start, end)); } return result; }
Example 11
Source File: FormattingRangesExtender.java From consulo with Apache License 2.0 | 5 votes |
@Nullable private TextRange trimSpaces(@Nonnull TextRange range) { int startOffset = range.getStartOffset(); int endOffset = range.getEndOffset(); startOffset = CharArrayUtil.shiftForward(myDocument.getCharsSequence(), startOffset, endOffset, " /t"); if (startOffset == endOffset) return null; endOffset = CharArrayUtil.shiftBackward(myDocument.getCharsSequence(), startOffset, endOffset, " /t"); return new TextRange(startOffset, endOffset); }
Example 12
Source File: EnterHandler.java From consulo with Apache License 2.0 | 5 votes |
@Nullable private PsiComment createJavaDocStub(final CodeInsightSettings settings, final PsiComment comment, final Project project) { if (settings.JAVADOC_STUB_ON_ENTER) { final DocumentationProvider langDocumentationProvider = LanguageDocumentation.INSTANCE.forLanguage(comment.getParent().getLanguage()); @Nullable final CodeDocumentationProvider docProvider; if (langDocumentationProvider instanceof CompositeDocumentationProvider) { docProvider = ((CompositeDocumentationProvider)langDocumentationProvider).getFirstCodeDocumentationProvider(); } else { docProvider = langDocumentationProvider instanceof CodeDocumentationProvider ? (CodeDocumentationProvider)langDocumentationProvider : null; } if (docProvider != null) { if (docProvider.findExistingDocComment(comment) != comment) return comment; String docStub; DumbService.getInstance(project).setAlternativeResolveEnabled(true); try { docStub = docProvider.generateDocumentationContentStub(comment); } finally { DumbService.getInstance(project).setAlternativeResolveEnabled(false); } if (docStub != null && docStub.length() != 0) { myOffset = CharArrayUtil.shiftForwardUntil(myDocument.getCharsSequence(), myOffset, LINE_SEPARATOR); myOffset = CharArrayUtil.shiftForward(myDocument.getCharsSequence(), myOffset, LINE_SEPARATOR); myDocument.insertString(myOffset, docStub); } } PsiDocumentManager.getInstance(project).commitAllDocuments(); return PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(myOffset), PsiComment.class); } return comment; }
Example 13
Source File: IndexPatternSearcher.java From consulo with Apache License 2.0 | 5 votes |
private static List<TextRange> findContinuation(int mainRangeStartOffset, CharSequence text, IndexPattern[] allIndexPatterns, List<? extends CommentRange> commentRanges, int commentNum) { CommentRange commentRange = commentRanges.get(commentNum); int lineStartOffset = CharArrayUtil.shiftBackwardUntil(text, mainRangeStartOffset - 1, "\n") + 1; int lineEndOffset = CharArrayUtil.shiftForwardUntil(text, mainRangeStartOffset, "\n"); int offsetInLine = mainRangeStartOffset - lineStartOffset; int maxCommentStartOffsetInLine = Math.max(0, commentRange.startOffset - lineStartOffset); List<TextRange> result = new ArrayList<>(); outer: while (lineEndOffset < text.length()) { lineStartOffset = lineEndOffset + 1; lineEndOffset = CharArrayUtil.shiftForwardUntil(text, lineStartOffset, "\n"); int refOffset = lineStartOffset + offsetInLine; int continuationStartOffset = CharArrayUtil.shiftForward(text, refOffset, lineEndOffset, WHITESPACE); if (continuationStartOffset == refOffset || continuationStartOffset >= lineEndOffset) break; if (continuationStartOffset >= commentRange.endOffset) { commentNum++; if (commentNum >= commentRanges.size()) break; commentRange = commentRanges.get(commentNum); if (continuationStartOffset < commentRange.startOffset || continuationStartOffset >= commentRange.endOffset) break; } int commentStartOffset = Math.max(lineStartOffset, commentRange.startOffset); int continuationEndOffset = Math.min(lineEndOffset, commentRange.endOffset); if (refOffset < commentStartOffset || commentStartOffset > lineStartOffset + maxCommentStartOffsetInLine || CharArrayUtil.shiftBackward(text, commentStartOffset, refOffset - 1, WHITESPACE + commentRange.allowedContinuationPrefixChars) + 1 != commentStartOffset) { break; } CharSequence commentText = StringPattern.newBombedCharSequence(text.subSequence(commentStartOffset, continuationEndOffset)); for (IndexPattern pattern : allIndexPatterns) { Pattern p = pattern.getPattern(); if (p != null && p.matcher(commentText).find()) break outer; } result.add(new TextRange(continuationStartOffset, continuationEndOffset)); } return result.isEmpty() ? Collections.emptyList() : result; }
Example 14
Source File: ConsoleLog.java From aem-ide-tooling-4-intellij with Apache License 2.0 | 5 votes |
private static void insertNewLineSubstitutors(Document document, AtomicBoolean showMore, List<RangeMarker> lineSeparators) { for(RangeMarker marker : lineSeparators) { if(!marker.isValid()) { showMore.set(true); continue; } int offset = marker.getStartOffset(); if(offset == 0 || offset == document.getTextLength()) { continue; } boolean spaceBefore = offset > 0 && Character.isWhitespace(document.getCharsSequence().charAt(offset - 1)); if(offset < document.getTextLength()) { boolean spaceAfter = Character.isWhitespace(document.getCharsSequence().charAt(offset)); int next = CharArrayUtil.shiftForward(document.getCharsSequence(), offset, " \t"); if(next < document.getTextLength() && !Character.isLowerCase(document.getCharsSequence().charAt(next))) { document.insertString(offset, (spaceBefore ? "" : " ") + "//" + (spaceAfter ? "" : " ")); continue; } if(spaceAfter) { continue; } } if(spaceBefore) { continue; } document.insertString(offset, " "); } }
Example 15
Source File: WidgetIndentsHighlightingPass.java From flutter-intellij with BSD 3-Clause "New" or "Revised" License | 5 votes |
boolean updateSelected(@NotNull Editor editor, @NotNull RangeHighlighter highlighter, Caret carat) { if (carat == null) { return setSelection(false); } final CaretModel caretModel = editor.getCaretModel(); final int startOffset = highlighter.getStartOffset(); final Document doc = highlighter.getDocument(); final int caretOffset = carat.getOffset(); if (startOffset < 0 || startOffset >= doc.getTextLength()) { return setSelection(false); } final int endOffset = highlighter.getEndOffset(); int off; int startLine = doc.getLineNumber(startOffset); { final CharSequence chars = doc.getCharsSequence(); do { final int start = doc.getLineStartOffset(startLine); final int end = doc.getLineEndOffset(startLine); off = CharArrayUtil.shiftForward(chars, start, end, " \t"); startLine--; } while (startLine > 1 && off < doc.getTextLength() && chars.charAt(off) == '\n'); } final VisualPosition startPosition = editor.offsetToVisualPosition(off); final int indentColumn = startPosition.column; final LogicalPosition logicalPosition = caretModel.getLogicalPosition(); if (logicalPosition.line == startLine + 1 && descriptor.widget != null) { // Be more permissive about what constitutes selection for the first // line within a widget constructor. return setSelection(caretModel.getLogicalPosition().column >= indentColumn); } return setSelection( caretOffset >= off && caretOffset < endOffset && caretModel.getLogicalPosition().column == indentColumn); }
Example 16
Source File: WidgetIndentsHighlightingPass.java From flutter-intellij with BSD 3-Clause "New" or "Revised" License | 5 votes |
boolean updateSelected(@NotNull Editor editor, @NotNull RangeHighlighter highlighter, Caret carat) { if (carat == null) { return setSelection(false); } final CaretModel caretModel = editor.getCaretModel(); final int startOffset = highlighter.getStartOffset(); final Document doc = highlighter.getDocument(); final int caretOffset = carat.getOffset(); if (startOffset < 0 || startOffset >= doc.getTextLength()) { return setSelection(false); } final int endOffset = highlighter.getEndOffset(); int off; int startLine = doc.getLineNumber(startOffset); { final CharSequence chars = doc.getCharsSequence(); do { final int start = doc.getLineStartOffset(startLine); final int end = doc.getLineEndOffset(startLine); off = CharArrayUtil.shiftForward(chars, start, end, " \t"); startLine--; } while (startLine > 1 && off < doc.getTextLength() && chars.charAt(off) == '\n'); } final VisualPosition startPosition = editor.offsetToVisualPosition(off); final int indentColumn = startPosition.column; final LogicalPosition logicalPosition = caretModel.getLogicalPosition(); if (logicalPosition.line == startLine + 1 && descriptor.widget != null) { // Be more permissive about what constitutes selection for the first // line within a widget constructor. return setSelection(caretModel.getLogicalPosition().column >= indentColumn); } return setSelection( caretOffset >= off && caretOffset < endOffset && caretModel.getLogicalPosition().column == indentColumn); }
Example 17
Source File: ParameterInfoController.java From consulo with Apache License 2.0 | 4 votes |
private static int getParameterNavigationOffset(@Nonnull PsiElement parameter, @Nonnull CharSequence text) { int rangeStart = parameter.getTextRange().getStartOffset(); int rangeEnd = parameter.getTextRange().getEndOffset(); int offset = CharArrayUtil.shiftBackward(text, rangeEnd - 1, WHITESPACE) + 1; return offset > rangeStart ? offset : CharArrayUtil.shiftForward(text, rangeEnd, WHITESPACE); }
Example 18
Source File: CamelHumpMatcher.java From consulo with Apache License 2.0 | 4 votes |
private static int skipUnderscores(@Nonnull String name) { return CharArrayUtil.shiftForward(name, 0, "_"); }
Example 19
Source File: SmartIndentingBackspaceHandler.java From consulo with Apache License 2.0 | 4 votes |
@Override protected void doBeforeCharDeleted(char c, PsiFile file, Editor editor) { Project project = file.getProject(); Document document = editor.getDocument(); CharSequence charSequence = document.getImmutableCharSequence(); CaretModel caretModel = editor.getCaretModel(); int caretOffset = caretModel.getOffset(); LogicalPosition pos = caretModel.getLogicalPosition(); int lineStartOffset = document.getLineStartOffset(pos.line); int beforeWhitespaceOffset = CharArrayUtil.shiftBackward(charSequence, caretOffset - 1, " \t") + 1; if (beforeWhitespaceOffset != lineStartOffset) { myReplacement = null; return; } PsiDocumentManager.getInstance(project).commitDocument(document); CodeStyleFacade codeStyleFacade = CodeStyleFacade.getInstance(project); myReplacement = codeStyleFacade.getLineIndent(document, lineStartOffset); if (myReplacement == null) { return; } int tabSize = codeStyleFacade.getTabSize(file.getFileType()); int targetColumn = getWidth(myReplacement, tabSize); int endOffset = CharArrayUtil.shiftForward(charSequence, caretOffset, " \t"); LogicalPosition logicalPosition = caretOffset < endOffset ? editor.offsetToLogicalPosition(endOffset) : pos; int currentColumn = logicalPosition.column; if (currentColumn > targetColumn) { myStartOffset = lineStartOffset; } else if (logicalPosition.line == 0) { myStartOffset = 0; myReplacement = ""; } else { int prevLineEndOffset = document.getLineEndOffset(logicalPosition.line - 1); myStartOffset = CharArrayUtil.shiftBackward(charSequence, prevLineEndOffset - 1, " \t") + 1; if (myStartOffset != document.getLineStartOffset(logicalPosition.line - 1)) { int spacing = CodeStyleManager.getInstance(project).getSpacing(file, endOffset); myReplacement = StringUtil.repeatSymbol(' ', Math.max(0, spacing)); } } }
Example 20
Source File: EnterBetweenBracesHandler.java From consulo with Apache License 2.0 | 4 votes |
@Override public Result preprocessEnter(@Nonnull final PsiFile file, @Nonnull final Editor editor, @Nonnull final Ref<Integer> caretOffsetRef, @Nonnull final Ref<Integer> caretAdvance, @Nonnull final DataContext dataContext, final EditorActionHandler originalHandler) { Document document = editor.getDocument(); CharSequence text = document.getCharsSequence(); int caretOffset = caretOffsetRef.get().intValue(); if (!CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER) { return Result.Continue; } int prevCharOffset = CharArrayUtil.shiftBackward(text, caretOffset - 1, " \t"); int nextCharOffset = CharArrayUtil.shiftForward(text, caretOffset, " \t"); if (!isValidOffset(prevCharOffset, text) || !isValidOffset(nextCharOffset, text) || !isBracePair(text.charAt(prevCharOffset), text.charAt(nextCharOffset))) { return Result.Continue; } PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument()); if (file.findElementAt(prevCharOffset) == file.findElementAt(nextCharOffset)) { return Result.Continue; } final int line = document.getLineNumber(caretOffset); final int start = document.getLineStartOffset(line); final CodeDocumentationUtil.CommentContext commentContext = CodeDocumentationUtil.tryParseCommentContext(file, text, caretOffset, start); // special case: enter inside "()" or "{}" String indentInsideJavadoc = isInComment(caretOffset, file) && commentContext.docAsterisk ? CodeDocumentationUtil.getIndentInsideJavadoc(document, caretOffset) : null; originalHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), dataContext); Project project = editor.getProject(); if (indentInsideJavadoc != null && project != null && CodeStyleSettingsManager.getSettings(project).JD_LEADING_ASTERISKS_ARE_ENABLED) { document.insertString(editor.getCaretModel().getOffset(), "*" + indentInsideJavadoc); } PsiDocumentManager.getInstance(file.getProject()).commitDocument(document); try { CodeStyleManager.getInstance(file.getProject()).adjustLineIndent(file, editor.getCaretModel().getOffset()); } catch (IncorrectOperationException e) { LOG.error(e); } return indentInsideJavadoc == null ? Result.Continue : Result.DefaultForceIndent; }