Java Code Examples for com.intellij.openapi.util.text.StringUtil#isJavaIdentifierStart()

The following examples show how to use com.intellij.openapi.util.text.StringUtil#isJavaIdentifierStart() . 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: ClassPrefixValidator.java    From json2java4idea with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canClose(@Nullable String prefix) {
    if (Strings.isNullOrEmpty(prefix)) {
        return true;
    }

    if (!nameHelper.isIdentifier(prefix)) {
        return false;
    }

    final char first = prefix.charAt(0);
    return StringUtil.isJavaIdentifierStart(first);
}
 
Example 2
Source File: JBIterator.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
static String toShortString(@Nonnull Object o) {
  String name = o.getClass().getName();
  int idx = name.lastIndexOf('$');
  if (idx > 0 && idx < name.length() && StringUtil.isJavaIdentifierStart(name.charAt(idx + 1))) {
    return name.substring(idx + 1);
  }
  return name.substring(name.lastIndexOf('.') + 1);
}
 
Example 3
Source File: GeneratedParserUtilBase.java    From Intellij-Dust with MIT License 5 votes vote down vote up
private boolean addExpected(StringBuilder sb, int offset, boolean expected) {
    String[] strings = new String[variants.size()];
    long[] hashes = new long[strings.length];
    Arrays.fill(strings, "");
    int count = 0;
    loop: for (Variant variant : expected? variants : unexpected) {
        if (offset == variant.offset) {
            String text = variant.object.toString();
            long hash = StringHash.calc(text);
            for (int i=0; i<count; i++) {
                if (hashes[i] == hash) continue loop;
            }
            hashes[count] = hash;
            strings[count] = text;
            count++;
        }
    }
    Arrays.sort(strings);
    count = 0;
    for (String s : strings) {
        if (s == "") continue;
        if (count++ > 0) {
            if (count > MAX_VARIANTS_TO_DISPLAY) {
                sb.append(" and ...");
                break;
            }
            else {
                sb.append(", ");
            }
        }
        char c = s.charAt(0);
        String displayText = c == '<' || StringUtil.isJavaIdentifierStart(c) ? s : '\'' + s + '\'';
        sb.append(displayText);
    }
    if (count > 1 && count < MAX_VARIANTS_TO_DISPLAY) {
        int idx = sb.lastIndexOf(", ");
        sb.replace(idx, idx + 1, " or");
    }
    return count > 0;
}
 
Example 4
Source File: GeneratedParserUtilBase.java    From intellij-latte with MIT License 4 votes vote down vote up
private boolean addExpected(StringBuilder sb, int position, boolean expected) {
	MyList<Variant> list = expected ? variants : unexpected;
	String[] strings = new String[list.size()];
	long[] hashes = new long[strings.length];
	Arrays.fill(strings, "");
	int count = 0;
	loop: for (Variant variant : list) {
		if (position == variant.position) {
			String text = variant.object.toString();
			long hash = StringHash.calc(text);
			for (int i=0; i<count; i++) {
				if (hashes[i] == hash) continue loop;
			}
			hashes[count] = hash;
			strings[count] = text;
			count++;
		}
	}
	Arrays.sort(strings);
	count = 0;
	for (String s : strings) {
		if (s.length() == 0) continue;
		if (count++ > 0) {
			if (count > MAX_VARIANTS_TO_DISPLAY) {
				sb.append(" and ...");
				break;
			}
			else {
				sb.append(", ");
			}
		}
		char c = s.charAt(0);
		String displayText = c == '<' || StringUtil.isJavaIdentifierStart(c) ? s : '\'' + s + '\'';
		sb.append(displayText);
	}
	if (count > 1 && count < MAX_VARIANTS_TO_DISPLAY) {
		int idx = sb.lastIndexOf(", ");
		sb.replace(idx, idx + 1, " or");
	}
	return count > 0;
}
 
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);
}