Java Code Examples for com.intellij.openapi.util.Segment#getStartOffset()

The following examples show how to use com.intellij.openapi.util.Segment#getStartOffset() . 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: FocusModeModel.java    From consulo with Apache License 2.0 6 votes vote down vote up
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 2
Source File: InjectedLanguageManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: TextRangeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
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 4
Source File: ShredImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@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 5
Source File: ChunkExtractor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
private TextChunk[] extractChunks(@Nonnull UsageInfo2UsageAdapter usageInfo2UsageAdapter, @Nonnull PsiFile file) {
  int absoluteStartOffset = usageInfo2UsageAdapter.getNavigationOffset();
  if (absoluteStartOffset == -1) return TextChunk.EMPTY_ARRAY;

  Document visibleDocument = myDocument instanceof DocumentWindow ? ((DocumentWindow)myDocument).getDelegate() : myDocument;
  int visibleStartOffset = myDocument instanceof DocumentWindow ? ((DocumentWindow)myDocument).injectedToHost(absoluteStartOffset) : absoluteStartOffset;

  int lineNumber = myDocument.getLineNumber(absoluteStartOffset);
  int visibleLineNumber = visibleDocument.getLineNumber(visibleStartOffset);
  int visibleColumnNumber = visibleStartOffset - visibleDocument.getLineStartOffset(visibleLineNumber);
  final List<TextChunk> result = new ArrayList<TextChunk>();
  appendPrefix(result, visibleLineNumber, visibleColumnNumber);

  int fragmentToShowStart = myDocument.getLineStartOffset(lineNumber);
  int fragmentToShowEnd = fragmentToShowStart < myDocument.getTextLength() ? myDocument.getLineEndOffset(lineNumber) : 0;
  if (fragmentToShowStart > fragmentToShowEnd) return TextChunk.EMPTY_ARRAY;

  final CharSequence chars = myDocument.getCharsSequence();
  if (fragmentToShowEnd - fragmentToShowStart > MAX_LINE_LENGTH_TO_SHOW) {
    final int lineStartOffset = fragmentToShowStart;
    fragmentToShowStart = Math.max(lineStartOffset, absoluteStartOffset - OFFSET_BEFORE_TO_SHOW_WHEN_LONG_LINE);

    final int lineEndOffset = fragmentToShowEnd;
    Segment segment = usageInfo2UsageAdapter.getUsageInfo().getSegment();
    int usage_length = segment != null ? segment.getEndOffset() - segment.getStartOffset() : 0;
    fragmentToShowEnd = Math.min(lineEndOffset, absoluteStartOffset + usage_length + OFFSET_AFTER_TO_SHOW_WHEN_LONG_LINE);

    // if we search something like a word, then expand shown context from one symbol before / after at least for word boundary
    // this should not cause restarts of the lexer as the tokens are usually words
    if (usage_length > 0 && StringUtil.isJavaIdentifierStart(chars.charAt(absoluteStartOffset)) && StringUtil.isJavaIdentifierStart(chars.charAt(absoluteStartOffset + usage_length - 1))) {
      while (fragmentToShowEnd < lineEndOffset && StringUtil.isJavaIdentifierStart(chars.charAt(fragmentToShowEnd - 1))) ++fragmentToShowEnd;
      while (fragmentToShowStart > lineStartOffset && StringUtil.isJavaIdentifierStart(chars.charAt(fragmentToShowStart))) --fragmentToShowStart;
      if (fragmentToShowStart != lineStartOffset) ++fragmentToShowStart;
      if (fragmentToShowEnd != lineEndOffset) --fragmentToShowEnd;
    }
  }
  if (myDocument instanceof DocumentWindow) {
    List<TextRange> editable = InjectedLanguageManager.getInstance(file.getProject()).intersectWithAllEditableFragments(file, new TextRange(fragmentToShowStart, fragmentToShowEnd));
    for (TextRange range : editable) {
      createTextChunks(usageInfo2UsageAdapter, chars, range.getStartOffset(), range.getEndOffset(), true, result);
    }
    return result.toArray(new TextChunk[result.size()]);
  }
  return createTextChunks(usageInfo2UsageAdapter, chars, fragmentToShowStart, fragmentToShowEnd, true, result);
}
 
Example 6
Source File: ShowIntentionsPass.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean isEmpty(@Nonnull Segment segment) {
  return segment.getEndOffset() <= segment.getStartOffset();
}
 
Example 7
Source File: FindResultUsageInfo.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isValid() {
  if (!super.isValid()) return false;

  Document document = PsiDocumentManager.getInstance(getProject()).getDocument(getPsiFile());
  if (document == null) {
    myCachedResult = null;
    return false;
  }

  Boolean cachedResult = myCachedResult;
  if (document.getModificationStamp() == myTimestamp && cachedResult != null) {
    return cachedResult;
  }
  myTimestamp = document.getModificationStamp();

  Segment segment = getSegment();
  if (segment == null) {
    myCachedResult = false;
    return false;
  }

  VirtualFile file = getPsiFile().getVirtualFile();

  Segment searchOffset;
  if (myAnchor != null) {
    searchOffset = myAnchor.getRange();
    if (searchOffset == null) {
      myCachedResult = false;
      return false;
    }
  }
  else {
    searchOffset = segment;
  }

  int offset = searchOffset.getStartOffset();
  Long data = myFindModel.getUserData(ourDocumentTimestampKey);
  if (data == null || data != myTimestamp) {
    data = myTimestamp;
    FindManagerImpl.clearPreviousFindData(myFindModel);
  }
  myFindModel.putUserData(ourDocumentTimestampKey, data);
  FindResult result;
  do {
    result = myFindManager.findString(document.getCharsSequence(), offset, myFindModel, file);
    offset = result.getEndOffset() == offset ? offset + 1 : result.getEndOffset();
    if (!result.isStringFound()) {
      myCachedResult = false;
      return false;
    }
  } while (result.getStartOffset() < segment.getStartOffset());

  boolean ret = segment.getStartOffset() == result.getStartOffset() && segment.getEndOffset() == result.getEndOffset();
  myCachedResult = ret;
  return ret;
}