com.intellij.lexer.StringLiteralLexer Java Examples

The following examples show how to use com.intellij.lexer.StringLiteralLexer. 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: CSharpEditorHighlighter.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
public CSharpEditorHighlighter(@Nullable final VirtualFile virtualFile, @Nonnull final EditorColorsScheme colors)
{
	super(new CSharpSyntaxHighlighter(), colors);

	registerLayer(CSharpCfsLanguageVersion.getInstance().getExpressionElementType(), new LayerDescriptor(new CSharpSyntaxHighlighter(), ""));

	registerLayer(CSharpTokens.STRING_LITERAL, new LayerDescriptor(new CSharpSyntaxHighlighter()
	{
		@Nonnull
		@Override
		public Lexer getHighlightingLexer()
		{
			return new StringLiteralLexer('\"', CSharpTokens.STRING_LITERAL);
		}
	}, ""));
	registerLayer(CSharpTokensImpl.LINE_DOC_COMMENT, new LayerDescriptor(new CSharpDocSyntaxHighlighter(), ""));
	registerLayer(CSharpTokens.CHARACTER_LITERAL, new LayerDescriptor(new CSharpSyntaxHighlighter()
	{
		@Nonnull
		@Override
		public Lexer getHighlightingLexer()
		{
			return new StringLiteralLexer('\'', CSharpTokens.CHARACTER_LITERAL);
		}
	}, ""));
	registerLayer(CSharpTokensImpl.INTERPOLATION_STRING_LITERAL, new LayerDescriptor(new CfsSyntaxHighlighter(CSharpCfsLanguageVersion.getInstance())
	{
		@Nonnull
		@Override
		public TextAttributesKey[] getTokenHighlights(IElementType elementType)
		{
			if(elementType == CfsTokens.TEXT)
			{
				return pack(CSharpHighlightKey.STRING);
			}
			return super.getTokenHighlights(elementType);
		}
	}, ""));
	registerLayer(CSharpTemplateTokens.PREPROCESSOR_FRAGMENT, new LayerDescriptor(new CSharpPreprocessorSyntaxHighlighter(), ""));
}
 
Example #2
Source File: CppHighlighter.java    From CppTools with Apache License 2.0 5 votes vote down vote up
@NotNull
public Lexer getHighlightingLexer() {
  return new LayeredLexer( new FlexAdapter(new _CppLexer(true, false, true, true, true)) ) { // TODO: c/c++ dialects
    {
      registerSelfStoppingLayer(new StringLiteralLexer('\"', CppTokenTypes.STRING_LITERAL),
                            new IElementType[]{CppTokenTypes.STRING_LITERAL}, IElementType.EMPTY_ARRAY);

      registerSelfStoppingLayer(new StringLiteralLexer('\'', CppTokenTypes.SINGLE_QUOTE_STRING_LITERAL),
                            new IElementType[]{CppTokenTypes.SINGLE_QUOTE_STRING_LITERAL},
                            IElementType.EMPTY_ARRAY);
    }
  };
}
 
Example #3
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;
}