Java Code Examples for com.intellij.openapi.util.ProperTextRange#create()

The following examples show how to use com.intellij.openapi.util.ProperTextRange#create() . 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: SmartPointerManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public SmartPsiFileRange createSmartPsiFileRangePointer(@Nonnull PsiFile file, @Nonnull TextRange range, boolean forInjected) {
  PsiUtilCore.ensureValid(file);
  SmartPointerTracker.processQueue();
  SmartPsiFileRangePointerImpl pointer = new SmartPsiFileRangePointerImpl(this, file, ProperTextRange.create(range), forInjected);
  trackPointer(pointer, file.getViewProvider().getVirtualFile());

  return pointer;
}
 
Example 2
Source File: MarkerCache.java    From consulo with Apache License 2.0 5 votes vote down vote up
@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 3
Source File: ChangedPsiRangeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
static ProperTextRange getChangedPsiRange(@Nonnull PsiFile file, @Nonnull Document document, @Nonnull CharSequence oldDocumentText, @Nonnull CharSequence newDocumentText) {
  int psiLength = oldDocumentText.length();
  if (!file.getViewProvider().supportsIncrementalReparse(file.getLanguage())) {
    return new ProperTextRange(0, psiLength);
  }
  List<DocumentEvent> events = ((PsiDocumentManagerBase)PsiDocumentManager.getInstance(file.getProject())).getEventsSinceCommit(document);
  int prefix = Integer.MAX_VALUE;
  int suffix = Integer.MAX_VALUE;
  int lengthBeforeEvent = psiLength;
  for (DocumentEvent event : events) {
    prefix = Math.min(prefix, event.getOffset());
    suffix = Math.min(suffix, lengthBeforeEvent - event.getOffset() - event.getOldLength());
    lengthBeforeEvent = lengthBeforeEvent - event.getOldLength() + event.getNewLength();
  }
  if ((prefix == psiLength || suffix == psiLength) && newDocumentText.length() == psiLength) {
    return null;
  }
  //Important! delete+insert sequence can give some of same chars back, lets grow affixes to include them.
  int shortestLength = Math.min(psiLength, newDocumentText.length());
  while (prefix < shortestLength && oldDocumentText.charAt(prefix) == newDocumentText.charAt(prefix)) {
    prefix++;
  }
  while (suffix < shortestLength - prefix && oldDocumentText.charAt(psiLength - suffix - 1) == newDocumentText.charAt(newDocumentText.length() - suffix - 1)) {
    suffix++;
  }
  int end = Math.max(prefix, psiLength - suffix);
  if (end == prefix && newDocumentText.length() == oldDocumentText.length()) return null;
  return ProperTextRange.create(prefix, end);
}
 
Example 4
Source File: ChameleonSyntaxHighlightingPass.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public TextEditorHighlightingPass createHighlightingPass(@Nonnull PsiFile file, @Nonnull Editor editor) {
  Project project = file.getProject();
  TextRange restrict = FileStatusMap.getDirtyTextRange(editor, Pass.UPDATE_ALL);
  if (restrict == null) return new ProgressableTextEditorHighlightingPass.EmptyPass(project, editor.getDocument());
  ProperTextRange priority = VisibleHighlightingPassFactory.calculateVisibleRange(editor);
  return new ChameleonSyntaxHighlightingPass(project, file, editor.getDocument(), ProperTextRange.create(restrict), priority, editor, new DefaultHighlightInfoProcessor());
}
 
Example 5
Source File: InjectedGeneralHighlightingPass.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * 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 6
Source File: TemplateManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static OffsetsInFile insertDummyIdentifierWithCache(PsiFile file, int startOffset, int endOffset, String replacement) {
  ProperTextRange editRange = ProperTextRange.create(startOffset, endOffset);
  assertRangeWithinDocument(editRange, file.getViewProvider().getDocument());

  ConcurrentMap<Pair<ProperTextRange, String>, OffsetsInFile> map = CachedValuesManager.getCachedValue(file, () -> CachedValueProvider.Result
          .create(ConcurrentFactoryMap.createMap(key -> copyWithDummyIdentifier(new OffsetsInFile(file), key.first.getStartOffset(), key.first.getEndOffset(), key.second)), file,
                  file.getViewProvider().getDocument()));
  return map.get(Pair.create(editRange, replacement));
}
 
Example 7
Source File: DesktopEditorErrorPanelUI.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void paint(Graphics g, JComponent c) {
  Rectangle componentBounds = c.getBounds();
  ProperTextRange docRange = ProperTextRange.create(0, componentBounds.height);
  if (myCachedTrack == null || myCachedHeight != componentBounds.height) {
    myCachedTrack = UIUtil.createImage(c, componentBounds.width, componentBounds.height, BufferedImage.TYPE_INT_ARGB);
    myCachedHeight = componentBounds.height;

    myDirtyYPositions = docRange;

    paintBackground(myCachedTrack.getGraphics(), new Rectangle(0, 0, componentBounds.width, componentBounds.height));
  }

  if (myDirtyYPositions == WHOLE_DOCUMENT) {
    myDirtyYPositions = docRange;
  }

  if (myDirtyYPositions != null) {
    final Graphics2D imageGraphics = myCachedTrack.createGraphics();

    ((ApplicationEx2)Application.get()).editorPaintStart();

    try {
      myDirtyYPositions = myDirtyYPositions.intersection(docRange);
      if (myDirtyYPositions == null) myDirtyYPositions = docRange;
      repaint(imageGraphics, componentBounds.width, myDirtyYPositions);
      myDirtyYPositions = null;
    }
    finally {
      ((ApplicationEx2)Application.get()).editorPaintFinish();
    }
  }

  UIUtil.drawImage(g, myCachedTrack, null, 0, 0);

  if(myPanel.isSmallIconVisible()) {
    ErrorStripeRenderer errorStripeRenderer = myPanel.getMarkupModel().getErrorStripeRenderer();

    if (errorStripeRenderer != null) {
      errorStripeRenderer.paint(c, g, new Rectangle(JBUI.scale(2), JBUI.scale(2), errorStripeRenderer.getSquareSize(), errorStripeRenderer.getSquareSize()));
    }
  }
}
 
Example 8
Source File: AnchorElementInfo.java    From consulo with Apache License 2.0 4 votes vote down vote up
AnchorElementInfo(@Nonnull PsiElement anchor, @Nonnull PsiFile containingFile, @Nonnull Identikit.ByAnchor identikit) {
  super(ProperTextRange.create(anchor.getTextRange()), identikit, containingFile, false);
  myStubElementTypeAndId = pack(-1, null);
}