com.intellij.openapi.util.Segment Java Examples
The following examples show how to use
com.intellij.openapi.util.Segment.
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: PersistentRangeMarker.java From consulo with Apache License 2.0 | 6 votes |
@Nullable private static Pair.NonNull<TextRange, LinesCols> applyChange(@Nonnull DocumentEvent event, @Nonnull Segment range, int intervalStart, int intervalEnd, boolean greedyLeft, boolean greedyRight, boolean stickingToRight, @Nonnull LinesCols linesCols) { boolean shouldTranslateViaDiff = PersistentRangeMarkerUtil.shouldTranslateViaDiff(event, range.getStartOffset(), range.getEndOffset()); Pair.NonNull<TextRange, LinesCols> translated = null; if (shouldTranslateViaDiff) { translated = translateViaDiff((DocumentEventImpl)event, linesCols); } if (translated == null) { TextRange fallback = applyChange(event, intervalStart, intervalEnd, greedyLeft, greedyRight, stickingToRight); if (fallback == null) return null; LinesCols lc = storeLinesAndCols(event.getDocument(), fallback.getStartOffset(), fallback.getEndOffset()); if (lc == null) return null; translated = Pair.createNonNull(fallback, lc); } return translated; }
Example #2
Source File: SearchForUsagesRunnable.java From consulo with Apache License 2.0 | 6 votes |
private static void flashUsageScriptaculously(@Nonnull final Usage usage) { if (!(usage instanceof UsageInfo2UsageAdapter)) { return; } UsageInfo2UsageAdapter usageInfo = (UsageInfo2UsageAdapter)usage; Editor editor = usageInfo.openTextEditor(true); if (editor == null) return; TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(CodeInsightColors.BLINKING_HIGHLIGHTS_ATTRIBUTES); RangeBlinker rangeBlinker = new RangeBlinker(editor, attributes, 6); List<Segment> segments = new ArrayList<>(); Processor<Segment> processor = Processors.cancelableCollectProcessor(segments); usageInfo.processRangeMarkers(processor); rangeBlinker.resetMarkers(segments); rangeBlinker.startBlinking(); }
Example #3
Source File: InjectedSelfElementInfo.java From consulo with Apache License 2.0 | 6 votes |
@Override PsiElement restoreElement(@Nonnull SmartPointerManagerImpl manager) { PsiFile hostFile = myHostContext.getContainingFile(); if (hostFile == null || !hostFile.isValid()) return null; PsiElement hostContext = myHostContext.getElement(); if (hostContext == null) return null; Segment segment = myInjectedFileRangeInHostFile.getPsiRange(); if (segment == null) return null; PsiFile injectedPsi = getInjectedFileIn(hostContext, hostFile, TextRange.create(segment)); ProperTextRange rangeInInjected = hostToInjected(true, segment, injectedPsi, myAffixOffsets); if (rangeInInjected == null) return null; return myType.findPsiElement(injectedPsi, rangeInInjected.getStartOffset(), rangeInInjected.getEndOffset()); }
Example #4
Source File: ShredImpl.java From consulo with Apache License 2.0 | 6 votes |
@Nonnull private TextRange calcRangeInsideHostElement(boolean usePsiRange) { PsiLanguageInjectionHost host = getHost(); Segment psiRange = usePsiRange ? relevantRangeInHost.getPsiRange() : relevantRangeInHost.getRange(); TextRange textRange = psiRange == null ? null : TextRange.create(psiRange); if (host == null) { if (textRange != null) return textRange; Segment fromSP = usePsiRange ? hostElementPointer.getPsiRange() : hostElementPointer.getRange(); if (fromSP != null) return TextRange.create(fromSP); return new TextRange(0, 0); } TextRange hostTextRange = host.getTextRange(); textRange = textRange == null ? null : textRange.intersection(hostTextRange); if (textRange == null) return new ProperTextRange(0, hostTextRange.getLength()); return textRange.shiftLeft(hostTextRange.getStartOffset()); }
Example #5
Source File: InjectedLanguageManagerImpl.java From consulo with Apache License 2.0 | 6 votes |
@Nonnull @Override public List<TextRange> getNonEditableFragments(@Nonnull DocumentWindow window) { List<TextRange> result = new ArrayList<>(); int offset = 0; for (PsiLanguageInjectionHost.Shred shred : ((DocumentWindowImpl)window).getShreds()) { Segment hostRange = shred.getHostRangeMarker(); if (hostRange == null) continue; offset = appendRange(result, offset, shred.getPrefix().length()); offset += hostRange.getEndOffset() - hostRange.getStartOffset(); offset = appendRange(result, offset, shred.getSuffix().length()); } return result; }
Example #6
Source File: FocusModeModel.java From consulo with Apache License 2.0 | 6 votes |
private void applyFocusMode(@Nonnull Segment focusRange) { EditorColorsScheme scheme = ObjectUtils.notNull(myEditor.getColorsScheme(), EditorColorsManager.getInstance().getGlobalScheme()); Color background = scheme.getDefaultBackground(); //noinspection UseJBColor Color foreground = Registry.getColor(ColorUtil.isDark(background) ? "editor.focus.mode.color.dark" : "editor.focus.mode.color.light", Color.GRAY); TextAttributes attributes = new TextAttributes(foreground, background, background, LINE_UNDERSCORE, Font.PLAIN); myEditor.putUserData(FOCUS_MODE_ATTRIBUTES, attributes); MarkupModel markupModel = myEditor.getMarkupModel(); DocumentEx document = myEditor.getDocument(); int textLength = document.getTextLength(); int start = focusRange.getStartOffset(); int end = focusRange.getEndOffset(); if (start <= textLength) myFocusModeMarkup.add(markupModel.addRangeHighlighter(0, start, LAYER, attributes, EXACT_RANGE)); if (end <= textLength) myFocusModeMarkup.add(markupModel.addRangeHighlighter(end, textLength, LAYER, attributes, EXACT_RANGE)); myFocusModeRange = document.createRangeMarker(start, end); }
Example #7
Source File: ShredImpl.java From consulo with Apache License 2.0 | 6 votes |
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PsiLanguageInjectionHost.Shred shred = (PsiLanguageInjectionHost.Shred)o; PsiLanguageInjectionHost host = getHost(); Segment hostRangeMarker = getHostRangeMarker(); Segment hostRangeMarker2 = shred.getHostRangeMarker(); return host != null && host.equals(shred.getHost()) && prefix.equals(shred.getPrefix()) && suffix.equals(shred.getSuffix()) && rangeInDecodedPSI.equals(shred.getRange()) && hostRangeMarker != null && hostRangeMarker2 != null && TextRange.areSegmentsEqual(hostRangeMarker, hostRangeMarker2); }
Example #8
Source File: IntentionUsagePanel.java From consulo with Apache License 2.0 | 5 votes |
private void setupSpots(Document document) { List<Segment> markers = new ArrayList<Segment>(); while (true) { String text = document.getText(); final int spotStart = text.indexOf("<" + SPOT_MARKER + ">"); if (spotStart < 0) break; final int spotEnd = text.indexOf("</" + SPOT_MARKER + ">", spotStart); if (spotEnd < 0) break; document.deleteString(spotEnd, spotEnd + SPOT_MARKER.length() + 3); document.deleteString(spotStart, spotStart + SPOT_MARKER.length() + 2); Segment spotMarker = new Segment() { @Override public int getStartOffset() { return spotStart; } @Override public int getEndOffset() { return spotEnd - SPOT_MARKER.length() - 2; } }; markers.add(spotMarker); } myRangeBlinker.resetMarkers(markers); if (!markers.isEmpty()) { myRangeBlinker.startBlinking(); } }
Example #9
Source File: ShredImpl.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull ShredImpl withRange(@Nonnull TextRange rangeInDecodedPSI, @Nonnull TextRange rangeInHostElementPSI, @Nonnull PsiLanguageInjectionHost newHost) { SmartPsiFileRange rangeMarker = relevantRangeInHost; Segment oldRangeInHostElementPSI = calcRangeInsideHostElement(false); SmartPointerManagerImpl pointerManager = (SmartPointerManagerImpl)SmartPointerManager.getInstance(rangeMarker.getProject()); SmartPsiElementPointer<PsiLanguageInjectionHost> newHostPointer = pointerManager.createSmartPsiElementPointer(newHost, newHost.getContainingFile(), true); if (!rangeInHostElementPSI.equals(TextRange.create(oldRangeInHostElementPSI))) { Segment hostElementRange = newHostPointer.getRange(); rangeMarker = pointerManager.createSmartPsiFileRangePointer(rangeMarker.getContainingFile(), rangeInHostElementPSI.shiftRight(hostElementRange.getStartOffset()), true); } return new ShredImpl(rangeMarker, newHostPointer, prefix, suffix, rangeInDecodedPSI, usePsiRange, isOneLine); }
Example #10
Source File: ActionUsagePanel.java From consulo with Apache License 2.0 | 5 votes |
private void setupSpots(Document document) { List<Segment> markers = new ArrayList<>(); while (true) { String text = document.getText(); final int spotStart = text.indexOf("<" + SPOT_MARKER + ">"); if (spotStart < 0) break; final int spotEnd = text.indexOf("</" + SPOT_MARKER + ">", spotStart); if (spotEnd < 0) break; document.deleteString(spotEnd, spotEnd + SPOT_MARKER.length() + 3); document.deleteString(spotStart, spotStart + SPOT_MARKER.length() + 2); Segment spotMarker = new Segment() { @Override public int getStartOffset() { return spotStart; } @Override public int getEndOffset() { return spotEnd - SPOT_MARKER.length() - 2; } }; markers.add(spotMarker); } myRangeBlinker.resetMarkers(markers); if (!markers.isEmpty()) { myRangeBlinker.startBlinking(); } }
Example #11
Source File: FileElementInfo.java From consulo with Apache License 2.0 | 5 votes |
@Nullable @Override Segment getPsiRange(@Nonnull SmartPointerManagerImpl manager) { Document currentDoc = FileDocumentManager.getInstance().getCachedDocument(myVirtualFile); Document committedDoc = currentDoc == null ? null : ((PsiDocumentManagerBase)PsiDocumentManager.getInstance(myProject)).getLastCommittedDocument(currentDoc); return committedDoc == null ? getRange(manager) : new TextRange(0, committedDoc.getTextLength()); }
Example #12
Source File: MarkerCache.java From consulo with Apache License 2.0 | 5 votes |
@Nullable static Segment getUpdatedRange(@Nonnull PsiFile containingFile, @Nonnull Segment segment, boolean isSegmentGreedy, @Nonnull FrozenDocument frozen, @Nonnull List<? extends DocumentEvent> events) { SelfElementInfo info = new SelfElementInfo(ProperTextRange.create(segment), new Identikit() { @Nullable @Override public PsiElement findPsiElement(@Nonnull PsiFile file, int startOffset, int endOffset) { return null; } @Nonnull @Override public Language getFileLanguage() { throw new IllegalStateException(); } @Override public boolean isForPsiFile() { return false; } }, containingFile, isSegmentGreedy); List<SelfElementInfo> infos = Collections.singletonList(info); boolean greedy = info.isGreedy(); int start = info.getPsiStartOffset(); int end = info.getPsiEndOffset(); boolean surviveOnExternalChange = events.stream().anyMatch(event -> isWholeDocumentReplace(frozen, (DocumentEventImpl)event)); ManualRangeMarker marker = new ManualRangeMarker(start, end, greedy, greedy, surviveOnExternalChange, null); UpdatedRanges ranges = new UpdatedRanges(0, frozen, infos, new ManualRangeMarker[]{marker}); // NB: convert events from completion to whole doc change event to more precise translation List<DocumentEvent> newEvents = ContainerUtil.map(events, event -> isWholeDocumentReplace(frozen, (DocumentEventImpl)event) ? new DocumentEventImpl(event.getDocument(), event.getOffset(), event.getOldFragment(), event.getNewFragment(), event.getOldTimeStamp(), true, ((DocumentEventImpl)event).getInitialStartOffset(), ((DocumentEventImpl)event).getInitialOldLength()) : event); UpdatedRanges updated = applyEvents(newEvents, ranges); return updated.myMarkers[0]; }
Example #13
Source File: InjectedGeneralHighlightingPass.java From consulo with Apache License 2.0 | 5 votes |
/** * finds the first nearest text range * @param documentWindow * @param startOffset * @return null means invalid */ @Nullable private static TextRange findNearestTextRange(final DocumentWindow documentWindow, final int startOffset) { TextRange textRange = null; for (Segment marker : documentWindow.getHostRanges()) { TextRange curRange = ProperTextRange.create(marker); if (curRange.getStartOffset() > startOffset && textRange != null) break; textRange = curRange; } return textRange; }
Example #14
Source File: LineMarkersUtil.java From consulo with Apache License 2.0 | 5 votes |
static boolean processLineMarkers(@Nonnull Project project, @Nonnull Document document, @Nonnull Segment bounds, int group, // -1 for all @Nonnull Processor<? super LineMarkerInfo<?>> processor) { MarkupModelEx markupModel = (MarkupModelEx)DocumentMarkupModel.forDocument(document, project, true); return markupModel.processRangeHighlightersOverlappingWith(bounds.getStartOffset(), bounds.getEndOffset(), highlighter -> { LineMarkerInfo<?> info = getLineMarkerInfo(highlighter); return info == null || group != -1 && info.updatePass != group || processor.process(info); }); }
Example #15
Source File: SmartPointerTracker.java From consulo with Apache License 2.0 | 5 votes |
private static <E extends PsiElement> void updatePointerTarget(@Nonnull SmartPsiElementPointerImpl<E> pointer, @Nullable Segment pointerRange) { E cachedElement = pointer.getCachedElement(); if (cachedElement == null) { return; } boolean cachedValid = cachedElement.isValid(); if (cachedValid) { if (pointerRange == null) { // document change could be damaging, but if PSI survived after reparse, let's point to it ((SelfElementInfo)pointer.getElementInfo()).switchToAnchor(cachedElement); return; } // after reparse and its complex tree diff, the element might have "moved" to other range // but if an element of the same type can still be found at the old range, let's point there if (pointerRange.equals(cachedElement.getTextRange())) { return; } } E actual = pointer.doRestoreElement(); if (actual == null && cachedValid && ((SelfElementInfo)pointer.getElementInfo()).updateRangeToPsi(pointerRange, cachedElement)) { return; } if (actual != cachedElement) { pointer.cacheElement(actual); } }
Example #16
Source File: AnchorElementInfo.java From consulo with Apache License 2.0 | 5 votes |
@Override public Segment getRange(@Nonnull SmartPointerManagerImpl manager) { if (getStubId() != -1) { switchToTree(manager); } return super.getRange(manager); }
Example #17
Source File: FileElementInfo.java From consulo with Apache License 2.0 | 5 votes |
@Override Segment getRange(@Nonnull SmartPointerManagerImpl manager) { if (!myVirtualFile.isValid()) return null; Document document = FileDocumentManager.getInstance().getDocument(myVirtualFile); return document == null ? null : TextRange.from(0, document.getTextLength()); }
Example #18
Source File: TextRangeUtil.java From consulo with Apache License 2.0 | 5 votes |
public static int getDistance(@Nonnull Segment r2, @Nonnull Segment r1) { int s1 = r1.getStartOffset(); int e1 = r1.getEndOffset(); int s2 = r2.getStartOffset(); int e2 = r2.getEndOffset(); return Math.max(s1, s2) <= Math.min(e1, e2) ? 0 : Math.min(Math.abs(s1 - e2), Math.abs(s2 - e1)); }
Example #19
Source File: SoftWrapApplianceManager.java From consulo with Apache License 2.0 | 5 votes |
public void recalculate(@Nonnull List<? extends Segment> ranges) { if (myIsDirty) { return; } if (myVisibleAreaWidth <= 0) { myIsDirty = true; return; } Collections.sort(ranges, (o1, o2) -> { int startDiff = o1.getStartOffset() - o2.getStartOffset(); return startDiff == 0 ? o2.getEndOffset() - o1.getEndOffset() : startDiff; }); final int[] lastRecalculatedOffset = {0}; SoftWrapAwareDocumentParsingListenerAdapter listener = new SoftWrapAwareDocumentParsingListenerAdapter() { @Override public void onRecalculationEnd(@Nonnull IncrementalCacheUpdateEvent event) { lastRecalculatedOffset[0] = event.getActualEndOffset(); } }; myListeners.add(listener); try { for (Segment range : ranges) { int lastOffset = lastRecalculatedOffset[0]; if (range.getEndOffset() > lastOffset) { recalculateSoftWraps(new IncrementalCacheUpdateEvent(Math.max(range.getStartOffset(), lastOffset), range.getEndOffset(), myEditor)); } } } finally { myListeners.remove(listener); } onRecalculationEnd(); }
Example #20
Source File: ShredImpl.java From consulo with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings("HardCodedStringLiteral") public String toString() { PsiLanguageInjectionHost host = getHost(); Segment hostRange = getHostRangeMarker(); return "Shred " + (host == null ? null : host.getTextRange()) + ": " + host + " In host range: " + (hostRange != null ? "(" + hostRange.getStartOffset() + "," + hostRange.getEndOffset() + ");" : "invalid;") + " PSI range: " + rangeInDecodedPSI; }
Example #21
Source File: PsiDocumentManagerImpl.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull @Override List<BooleanRunnable> reparseChangedInjectedFragments(@Nonnull Document hostDocument, @Nonnull PsiFile hostPsiFile, @Nonnull TextRange hostChangedRange, @Nonnull ProgressIndicator indicator, @Nonnull ASTNode oldRoot, @Nonnull ASTNode newRoot) { List<DocumentWindow> changedInjected = InjectedLanguageManager.getInstance(myProject).getCachedInjectedDocumentsInRange(hostPsiFile, hostChangedRange); if (changedInjected.isEmpty()) return Collections.emptyList(); FileViewProvider hostViewProvider = hostPsiFile.getViewProvider(); List<DocumentWindow> fromLast = new ArrayList<>(changedInjected); // make sure modifications do not ruin all document offsets after fromLast.sort(Collections.reverseOrder(Comparator.comparingInt(doc -> ArrayUtil.getLastElement(doc.getHostRanges()).getEndOffset()))); List<BooleanRunnable> result = new ArrayList<>(changedInjected.size()); for (DocumentWindow document : fromLast) { Segment[] ranges = document.getHostRanges(); if (ranges.length != 0) { // host document change has left something valid in this document window place. Try to reparse. PsiFile injectedPsiFile = getCachedPsiFile(document); if (injectedPsiFile == null || !injectedPsiFile.isValid()) continue; BooleanRunnable runnable = InjectedLanguageUtil.reparse(injectedPsiFile, document, hostPsiFile, hostDocument, hostViewProvider, indicator, oldRoot, newRoot, this); ContainerUtil.addIfNotNull(result, runnable); } } return result; }
Example #22
Source File: InjectedSelfElementInfo.java From consulo with Apache License 2.0 | 5 votes |
@Override PsiFile restoreFile(@Nonnull SmartPointerManagerImpl manager) { PsiFile hostFile = myHostContext.getContainingFile(); if (hostFile == null || !hostFile.isValid()) return null; PsiElement hostContext = myHostContext.getElement(); if (hostContext == null) return null; Segment segment = myInjectedFileRangeInHostFile.getPsiRange(); if (segment == null) return null; final TextRange rangeInHostFile = TextRange.create(segment); return getInjectedFileIn(hostContext, hostFile, rangeInHostFile); }
Example #23
Source File: InjectedSelfElementInfo.java From consulo with Apache License 2.0 | 5 votes |
@Nullable private ProperTextRange getInjectedRange(boolean psi) { PsiElement hostContext = myHostContext.getElement(); if (hostContext == null) return null; Segment hostElementRange = psi ? myInjectedFileRangeInHostFile.getPsiRange() : myInjectedFileRangeInHostFile.getRange(); if (hostElementRange == null) return null; return hostToInjected(psi, hostElementRange, restoreFile((SmartPointerManagerImpl)SmartPointerManager.getInstance(getProject())), myAffixOffsets); }
Example #24
Source File: RangeBlinker.java From consulo with Apache License 2.0 | 5 votes |
public void startBlinking() { Project project = myEditor.getProject(); if (ApplicationManager.getApplication().isDisposed() || myEditor.isDisposed() || project != null && project.isDisposed()) { return; } MarkupModel markupModel = myEditor.getMarkupModel(); if (show) { for (Segment segment : myMarkers) { if (segment.getEndOffset() > myEditor.getDocument().getTextLength()) continue; RangeHighlighter highlighter = markupModel.addRangeHighlighter(segment.getStartOffset(), segment.getEndOffset(), HighlighterLayer.ADDITIONAL_SYNTAX, myAttributes, HighlighterTargetArea.EXACT_RANGE); myAddedHighlighters.add(highlighter); } } else { removeHighlights(); } stopBlinking(); myBlinkingAlarm.addRequest(new Runnable() { @Override public void run() { if (myTimeToLive > 0 || show) { myTimeToLive--; show = !show; startBlinking(); } } }, 400); }
Example #25
Source File: RangeBlinker.java From consulo with Apache License 2.0 | 5 votes |
public void resetMarkers(final List<Segment> markers) { removeHighlights(); myMarkers.clear(); stopBlinking(); myMarkers.addAll(markers); show = true; }
Example #26
Source File: ChunkExtractor.java From consulo with Apache License 2.0 | 5 votes |
private void processIntersectingRange(@Nonnull UsageInfo2UsageAdapter usageInfo2UsageAdapter, @Nonnull final CharSequence chars, int hiStart, final int hiEnd, @Nonnull final TextAttributesKey[] tokenHighlights, final boolean selectUsageWithBold, @Nonnull final List<TextChunk> result) { final TextAttributes originalAttrs = convertAttributes(tokenHighlights); if (selectUsageWithBold) { originalAttrs.setFontType(Font.PLAIN); } final int[] lastOffset = {hiStart}; usageInfo2UsageAdapter.processRangeMarkers(new Processor<Segment>() { @Override public boolean process(Segment segment) { int usageStart = segment.getStartOffset(); int usageEnd = segment.getEndOffset(); if (rangeIntersect(lastOffset[0], hiEnd, usageStart, usageEnd)) { addChunk(chars, lastOffset[0], Math.max(lastOffset[0], usageStart), originalAttrs, false, null, result); UsageType usageType = isHighlightedAsString(tokenHighlights) ? UsageType.LITERAL_USAGE : isHighlightedAsComment(tokenHighlights) ? UsageType.COMMENT_USAGE : null; addChunk(chars, Math.max(lastOffset[0], usageStart), Math.min(hiEnd, usageEnd), originalAttrs, selectUsageWithBold, usageType, result); lastOffset[0] = usageEnd; if (usageEnd > hiEnd) { return false; } } return true; } }); if (lastOffset[0] < hiEnd) { addChunk(chars, lastOffset[0], hiEnd, originalAttrs, false, null, result); } }
Example #27
Source File: DirElementInfo.java From consulo with Apache License 2.0 | 4 votes |
@Override Segment getRange(@Nonnull SmartPointerManagerImpl manager) { return null; }
Example #28
Source File: DirElementInfo.java From consulo with Apache License 2.0 | 4 votes |
@Nullable @Override Segment getPsiRange(@Nonnull SmartPointerManagerImpl manager) { return null; }
Example #29
Source File: FormatTextRanges.java From consulo with Apache License 2.0 | 4 votes |
public void setDisabledRanges(@Nonnull Collection<TextRange> disabledRanges) { myDisabledRanges.clear(); myDisabledRanges.addAll(ContainerUtil.sorted(disabledRanges, Segment.BY_START_OFFSET_THEN_END_OFFSET)); }
Example #30
Source File: ShowIntentionsPass.java From consulo with Apache License 2.0 | 4 votes |
private static boolean isEmpty(@Nonnull Segment segment) { return segment.getEndOffset() <= segment.getStartOffset(); }