com.intellij.psi.StringEscapesTokenTypes Java Examples

The following examples show how to use com.intellij.psi.StringEscapesTokenTypes. 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: CSharpQuoteHandler.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isOpeningQuote(HighlighterIterator iterator, int offset)
{
	boolean openingQuote = super.isOpeningQuote(iterator, offset);

	if(openingQuote)
	{
		// check escape next
		if(!iterator.atEnd())
		{
			iterator.retreat();

			if(!iterator.atEnd() && StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(iterator.getTokenType()))
			{
				openingQuote = false;
			}
			iterator.advance();
		}
	}
	return openingQuote;
}
 
Example #2
Source File: CSharpQuoteHandler.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isClosingQuote(HighlighterIterator iterator, int offset)
{
	boolean closingQuote = super.isClosingQuote(iterator, offset);

	if(closingQuote)
	{
		// check escape next
		if(!iterator.atEnd())
		{
			iterator.advance();

			if(!iterator.atEnd() && StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(iterator.getTokenType()))
			{
				closingQuote = false;
			}
			iterator.retreat();
		}
	}
	return closingQuote;
}
 
Example #3
Source File: SelectWordUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void addWordHonoringEscapeSequences(CharSequence editorText,
                                                  TextRange literalTextRange,
                                                  int cursorOffset,
                                                  Lexer lexer,
                                                  List<TextRange> result) {
  lexer.start(editorText, literalTextRange.getStartOffset(), literalTextRange.getEndOffset());

  while (lexer.getTokenType() != null) {
    if (lexer.getTokenStart() <= cursorOffset && cursorOffset < lexer.getTokenEnd()) {
      if (StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(lexer.getTokenType())) {
        result.add(new TextRange(lexer.getTokenStart(), lexer.getTokenEnd()));
      }
      else {
        TextRange word = getWordSelectionRange(editorText, cursorOffset, JAVA_IDENTIFIER_PART_CONDITION);
        if (word != null) {
          result.add(new TextRange(Math.max(word.getStartOffset(), lexer.getTokenStart()),
                                   Math.min(word.getEndOffset(), lexer.getTokenEnd())));
        }
      }
      break;
    }
    lexer.advance();
  }
}
 
Example #4
Source File: EnterInStringLiteralHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean isInStringLiteral(@Nonnull Editor editor, @Nonnull DataContext dataContext, int offset) {
  Language language = EnterHandler.getLanguage(dataContext);
  if (offset > 0 && language != null) {
    QuoteHandler quoteHandler = TypedHandler.getLanguageQuoteHandler(language);
    if (quoteHandler == null) {
      FileType fileType = language.getAssociatedFileType();
      quoteHandler = fileType != null ? TypedHandler.getQuoteHandlerForType(fileType) : null;
    }
    if (quoteHandler != null) {
      EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter();
      HighlighterIterator iterator = highlighter.createIterator(offset - 1);
      return StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(iterator.getTokenType()) || quoteHandler.isInsideLiteral(iterator);
    }
  }
  return false;
}
 
Example #5
Source File: LineLayout.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean distinctTokens(@Nullable IElementType token1, @Nullable IElementType token2) {
  if (token1 == token2) return false;
  if (token1 == null || token2 == null) return true;
  if (StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(token1) || StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(token2)) return false;
  if (token1 != TokenType.WHITE_SPACE && token2 != TokenType.WHITE_SPACE && !token1.getLanguage().is(token2.getLanguage())) return true;
  Language language = token1.getLanguage();
  if (language == Language.ANY) language = token2.getLanguage();
  BidiRegionsSeparator separator = LanguageBidiRegionsSeparator.INSTANCE.forLanguage(language);
  return separator.createBorderBetweenTokens(token1, token2);
}
 
Example #6
Source File: StringLiteralLexer.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public IElementType getTokenType() {
  if (myStart >= myEnd) return null;

  if (myBuffer.charAt(myStart) != '\\') {
    mySeenEscapedSpacesOnly = false;
    return myOriginalLiteralToken;
  }

  if (myStart + 1 >= myEnd) return StringEscapesTokenTypes.INVALID_CHARACTER_ESCAPE_TOKEN;
  char nextChar = myBuffer.charAt(myStart + 1);
  mySeenEscapedSpacesOnly &= nextChar == ' ';
  if (myCanEscapeEolOrFramingSpaces &&
      (nextChar == '\n' || nextChar == ' ' && (mySeenEscapedSpacesOnly || isTrailingSpace(myStart+2)))
          ) {
    return StringEscapesTokenTypes.VALID_STRING_ESCAPE_TOKEN;
  }
  if (nextChar == 'u') {
    for(int i = myStart + 2; i < myStart + 6; i++) {
      if (i >= myEnd || !StringUtil.isHexDigit(myBuffer.charAt(i))) return StringEscapesTokenTypes.INVALID_UNICODE_ESCAPE_TOKEN;
    }
    return StringEscapesTokenTypes.VALID_STRING_ESCAPE_TOKEN;
  }

  if (nextChar == 'x' && myAllowHex) {
    for(int i = myStart + 2; i < myStart + 4; i++) {
      if (i >= myEnd || !StringUtil.isHexDigit(myBuffer.charAt(i))) return StringEscapesTokenTypes.INVALID_UNICODE_ESCAPE_TOKEN;
    }
    return StringEscapesTokenTypes.VALID_STRING_ESCAPE_TOKEN;
  }

  switch (nextChar) {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
      if (!myAllowOctal) return StringEscapesTokenTypes.INVALID_CHARACTER_ESCAPE_TOKEN;
      //noinspection fallthrough
    case 'n':
    case 'r':
    case 'b':
    case 't':
    case 'f':
    case '\'':
    case '\"':
    case '\\':
      return StringEscapesTokenTypes.VALID_STRING_ESCAPE_TOKEN;
  }
  if (myAdditionalValidEscapes != null && myAdditionalValidEscapes.indexOf(nextChar) != -1) {
    return StringEscapesTokenTypes.VALID_STRING_ESCAPE_TOKEN;
  }

  return StringEscapesTokenTypes.INVALID_CHARACTER_ESCAPE_TOKEN;
}
 
Example #7
Source File: EnterInStringLiteralHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public Result preprocessEnter(@Nonnull final PsiFile file, @Nonnull final Editor editor, @Nonnull Ref<Integer> caretOffsetRef,
                              @Nonnull final Ref<Integer> caretAdvanceRef, @Nonnull final DataContext dataContext,
                              final EditorActionHandler originalHandler) {
  int caretOffset = caretOffsetRef.get().intValue();
  int caretAdvance = caretAdvanceRef.get().intValue();
  if (!isInStringLiteral(editor, dataContext, caretOffset)) return Result.Continue;
  PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
  PsiElement psiAtOffset = file.findElementAt(caretOffset);
  if (psiAtOffset != null && psiAtOffset.getTextOffset() < caretOffset) {
    Document document = editor.getDocument();
    CharSequence text = document.getText();
    ASTNode token = psiAtOffset.getNode();
    JavaLikeQuoteHandler quoteHandler = getJavaLikeQuoteHandler(editor, psiAtOffset);

    if (quoteHandler != null &&
        quoteHandler.getConcatenatableStringTokenTypes() != null &&
        quoteHandler.getConcatenatableStringTokenTypes().contains(token.getElementType())) {
      TextRange range = token.getTextRange();
      final char literalStart = token.getText().charAt(0);
      final StringLiteralLexer lexer = new StringLiteralLexer(literalStart, token.getElementType());
      lexer.start(text, range.getStartOffset(), range.getEndOffset());

      while (lexer.getTokenType() != null) {
        if (lexer.getTokenStart() < caretOffset && caretOffset < lexer.getTokenEnd()) {
          if (StringEscapesTokenTypes.STRING_LITERAL_ESCAPES.contains(lexer.getTokenType())) {
            caretOffset = lexer.getTokenEnd();
          }
          break;
        }
        lexer.advance();
      }

      if (quoteHandler.needParenthesesAroundConcatenation(psiAtOffset)) {
        document.insertString(psiAtOffset.getTextRange().getEndOffset(), ")");
        document.insertString(psiAtOffset.getTextRange().getStartOffset(), "(");
        caretOffset++;
        caretAdvance++;
      }

      final String insertedFragment = literalStart + " " + quoteHandler.getStringConcatenationOperatorRepresentation();
      document.insertString(caretOffset, insertedFragment + " " + literalStart);
      caretOffset += insertedFragment.length();
      caretAdvance = 1;
      CommonCodeStyleSettings langSettings =
              CodeStyleSettingsManager.getSettings(file.getProject()).getCommonSettings(file.getLanguage());
      if (langSettings.BINARY_OPERATION_SIGN_ON_NEXT_LINE) {
        caretOffset -= 1;
        caretAdvance = 3;
      }
      caretOffsetRef.set(caretOffset);
      caretAdvanceRef.set(caretAdvance);
      return Result.DefaultForceIndent;
    }
  }
  return Result.Continue;
}