private void highlightInjectedSyntax(
      @NotNull PsiFile injectedPsi, @NotNull HighlightInfoHolder holder) {
    List<Trinity<IElementType, SmartPsiElementPointer<PsiLanguageInjectionHost>, TextRange>>
        tokens = InjectedLanguageUtil.getHighlightTokens(injectedPsi);
    if (tokens == null) return;

    final Language injectedLanguage = injectedPsi.getLanguage();
    Project project = injectedPsi.getProject();
    SyntaxHighlighter syntaxHighlighter =
        SyntaxHighlighterFactory.getSyntaxHighlighter(
            injectedLanguage, project, injectedPsi.getVirtualFile());
    final TextAttributes defaultAttrs = myGlobalScheme.getAttributes(HighlighterColors.TEXT);

    for (Trinity<IElementType, SmartPsiElementPointer<PsiLanguageInjectionHost>, TextRange> token :
        tokens) {
      ProgressManager.checkCanceled();
      IElementType tokenType = token.getFirst();
      PsiLanguageInjectionHost injectionHost = token.getSecond().getElement();
      if (injectionHost == null) continue;
      TextRange textRange = token.getThird();
      TextAttributesKey[] keys = syntaxHighlighter.getTokenHighlights(tokenType);
      if (textRange.getLength() == 0) continue;

      TextRange annRange = textRange.shiftRight(injectionHost.getTextRange().getStartOffset());
      // force attribute colors to override host' ones
      TextAttributes attributes = null;
      for (TextAttributesKey key : keys) {
        TextAttributes attrs2 = myGlobalScheme.getAttributes(key);
        if (attrs2 != null) {
          attributes = attributes == null ? attrs2 : TextAttributes.merge(attributes, attrs2);
        }
      }
      TextAttributes forcedAttributes;
      if (attributes == null || attributes.isEmpty() || attributes.equals(defaultAttrs)) {
        forcedAttributes = TextAttributes.ERASE_MARKER;
      } else {
        HighlightInfo info =
            HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_FRAGMENT)
                .range(annRange)
                .textAttributes(TextAttributes.ERASE_MARKER)
                .createUnconditionally();
        holder.add(info);

        forcedAttributes =
            new TextAttributes(
                attributes.getForegroundColor(),
                attributes.getBackgroundColor(),
                attributes.getEffectColor(),
                attributes.getEffectType(),
                attributes.getFontType());
      }

      HighlightInfo info =
          HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_FRAGMENT)
              .range(annRange)
              .textAttributes(forcedAttributes)
              .createUnconditionally();
      holder.add(info);
    }
  }
 protected TextAttributes convertAttributes(@NotNull TextAttributesKey[] keys) {
   TextAttributes attrs = myScheme.getAttributes(HighlighterColors.TEXT);
   for (TextAttributesKey key : keys) {
     TextAttributes attrs2 = myScheme.getAttributes(key);
     if (attrs2 != null) {
       attrs = TextAttributes.merge(attrs, attrs2);
     }
   }
   return attrs;
 }
예제 #3
0
 @Nullable
 public TextAttributes getTextAttributes(@NotNull Editor editor) {
   TextAttributes originalAttrs = getTextAttributes(editor.getColorsScheme());
   if (originalAttrs == null) {
     return null;
   }
   TextAttributes overridingAttributes = new TextAttributes();
   if (myApplied) {
     overridingAttributes.setBackgroundColor(((EditorEx) editor).getBackgroundColor());
   } else if (myInlineWrapper) {
     overridingAttributes.setBackgroundColor(
         getBgColorForFragmentContainingInlines((EditorEx) editor));
   }
   return TextAttributes.merge(originalAttrs, overridingAttributes);
 }
예제 #4
0
public class PerlSyntaxHighlighter extends SyntaxHighlighterBase {
  public static final TextAttributes BOLD = new TextAttributes(null, null, null, null, Font.BOLD);
  public static final TextAttributes ITALIC =
      new TextAttributes(null, null, null, null, Font.ITALIC);
  public static final TextAttributes BOLD_ITALIC = TextAttributes.merge(BOLD, ITALIC);
  public static final TextAttributes STROKE =
      new TextAttributes(null, null, null, EffectType.STRIKEOUT, Font.PLAIN);

  public static final TextAttributesKey[] EMPTY_KEYS = new TextAttributesKey[0];

  public static final TextAttributesKey PERL_NUMBER =
      createTextAttributesKey("PERL_NUMBER", DefaultLanguageHighlighterColors.NUMBER);
  public static final TextAttributesKey PERL_VERSION =
      createTextAttributesKey("PERL_VERSION", PERL_NUMBER);

  public static final TextAttributesKey PERL_COMMENT =
      createTextAttributesKey("PERL_COMMENT", DefaultLanguageHighlighterColors.LINE_COMMENT);
  public static final TextAttributesKey PERL_COMMENT_BLOCK =
      createTextAttributesKey("PERL_COMMENT_BLOCK", DefaultLanguageHighlighterColors.BLOCK_COMMENT);

  public static final TextAttributesKey PERL_HANDLE =
      createTextAttributesKey("PERL_HANDLE", DefaultLanguageHighlighterColors.CONSTANT);

  public static final TextAttributesKey PERL_PACKAGE =
      createTextAttributesKey("PERL_PACKAGE", DefaultLanguageHighlighterColors.CLASS_NAME);
  public static final TextAttributesKey PERL_PACKAGE_DEFINITION =
      createTextAttributesKey("PERL_PACKAGE_DEFINITION", PERL_PACKAGE);

  public static final TextAttributesKey PERL_SUB =
      createTextAttributesKey("PERL_SUB", DefaultLanguageHighlighterColors.FUNCTION_CALL);
  public static final TextAttributesKey PERL_SUB_DEFINITION =
      createTextAttributesKey(
          "PERL_SUB_DEFINITION", DefaultLanguageHighlighterColors.FUNCTION_DECLARATION);
  public static final TextAttributesKey PERL_SUB_DECLARATION =
      createTextAttributesKey("PERL_SUB_DECLARATION", PERL_SUB_DEFINITION);

  public static final TextAttributesKey PERL_LABEL =
      createTextAttributesKey("PERL_LABEL", DefaultLanguageHighlighterColors.LABEL);
  public static final TextAttributesKey PERL_BLOCK_NAME =
      createTextAttributesKey(
          "PERL_BLOCK_NAME", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL);
  public static final TextAttributesKey PERL_TAG =
      createTextAttributesKey("PERL_TAG", DefaultLanguageHighlighterColors.PREDEFINED_SYMBOL);

  public static final TextAttributesKey PERL_KEYWORD =
      createTextAttributesKey("PERL_KEYWORD", DefaultLanguageHighlighterColors.KEYWORD);
  public static final TextAttributesKey PERL_OPERATOR =
      createTextAttributesKey("PERL_OPERATOR", DefaultLanguageHighlighterColors.OPERATION_SIGN);
  public static final TextAttributesKey PERL_DEREFERENCE =
      createTextAttributesKey("PERL_DEREFERENCE", DefaultLanguageHighlighterColors.OPERATION_SIGN);

  public static final TextAttributesKey PERL_REGEX_QUOTE =
      createTextAttributesKey("PERL_REGEX_QUOTE", DefaultLanguageHighlighterColors.BRACKETS);
  public static final TextAttributesKey PERL_REGEX_TOKEN =
      createTextAttributesKey("PERL_REGEX_TOKEN", DefaultLanguageHighlighterColors.STRING);

  public static final TextAttributesKey PERL_ANNOTATION =
      createTextAttributesKey("PERL_ANNOTATION", DefaultLanguageHighlighterColors.METADATA);
  public static final TextAttributesKey PERL_SUB_ATTRIBUTE =
      createTextAttributesKey("PERL_SUB_ATTRIBUTE", DefaultLanguageHighlighterColors.METADATA);
  public static final TextAttributesKey PERL_SUB_PROTOTYPE_TOKEN =
      createTextAttributesKey("PERL_SUB_PROTOTYPE", DefaultLanguageHighlighterColors.PARAMETER);

  public static final TextAttributesKey PERL_SQ_STRING =
      createTextAttributesKey("PERL_SQ_STRING", DefaultLanguageHighlighterColors.STRING);
  public static final TextAttributesKey PERL_DQ_STRING =
      createTextAttributesKey("PERL_DQ_STRING", DefaultLanguageHighlighterColors.STRING);
  public static final TextAttributesKey PERL_DX_STRING =
      createTextAttributesKey("PERL_DX_STRING", DefaultLanguageHighlighterColors.STRING);

  public static final TextAttributesKey PERL_COMMA =
      createTextAttributesKey("PERL_COMMA", DefaultLanguageHighlighterColors.COMMA);
  public static final TextAttributesKey PERL_SEMICOLON =
      createTextAttributesKey("PERL_SEMICOLON", DefaultLanguageHighlighterColors.SEMICOLON);
  public static final TextAttributesKey PERL_BRACE =
      createTextAttributesKey("PERL_BRACES", DefaultLanguageHighlighterColors.BRACES);
  public static final TextAttributesKey PERL_PAREN =
      createTextAttributesKey("PERL_PARENTESS", DefaultLanguageHighlighterColors.PARENTHESES);
  public static final TextAttributesKey PERL_BRACK =
      createTextAttributesKey("PERL_BRACKETS", DefaultLanguageHighlighterColors.BRACKETS);
  public static final TextAttributesKey PERL_ANGLE =
      createTextAttributesKey("PERL_ANGLES", DefaultLanguageHighlighterColors.BRACKETS);

  public static final TextAttributesKey PERL_SCALAR =
      createTextAttributesKey("PERL_SCALAR", DefaultLanguageHighlighterColors.IDENTIFIER);
  public static final TextAttributesKey PERL_ARRAY =
      createTextAttributesKey("PERL_ARRAY", DefaultLanguageHighlighterColors.IDENTIFIER);
  public static final TextAttributesKey PERL_HASH =
      createTextAttributesKey("PERL_HASH", DefaultLanguageHighlighterColors.IDENTIFIER);
  public static final TextAttributesKey PERL_GLOB =
      createTextAttributesKey("PERL_GLOB", DefaultLanguageHighlighterColors.IDENTIFIER);

  private static final HashMap<IElementType, TextAttributesKey[]> attributesMap = new HashMap<>();

  static {
    attributesMap.put(
        PerlElementTypes.ANNOTATION_ABSTRACT_KEY, new TextAttributesKey[] {PERL_ANNOTATION});
    attributesMap.put(
        PerlElementTypes.ANNOTATION_DEPRECATED_KEY, new TextAttributesKey[] {PERL_ANNOTATION});
    attributesMap.put(
        PerlElementTypes.ANNOTATION_METHOD_KEY, new TextAttributesKey[] {PERL_ANNOTATION});
    attributesMap.put(
        PerlElementTypes.ANNOTATION_OVERRIDE_KEY, new TextAttributesKey[] {PERL_ANNOTATION});
    attributesMap.put(
        PerlElementTypes.ANNOTATION_PREFIX, new TextAttributesKey[] {PERL_ANNOTATION});
    attributesMap.put(
        PerlElementTypes.ANNOTATION_RETURNS_KEY, new TextAttributesKey[] {PERL_ANNOTATION});
    attributesMap.put(
        PerlElementTypes.ANNOTATION_UNKNOWN_KEY, new TextAttributesKey[] {PERL_ANNOTATION});

    attributesMap.put(PerlElementTypes.PERL_ARROW_COMMA, new TextAttributesKey[] {PERL_COMMA});

    attributesMap.put(PerlElementTypes.PERL_BLOCK_NAME, new TextAttributesKey[] {PERL_KEYWORD});

    attributesMap.put(PerlElementTypes.PERL_COLON, new TextAttributesKey[] {PERL_OPERATOR});
    attributesMap.put(PerlElementTypes.PERL_COMMA, new TextAttributesKey[] {PERL_COMMA});

    attributesMap.put(PerlElementTypes.PERL_COMMENT, new TextAttributesKey[] {PERL_COMMENT});
    attributesMap.put(PerlElementTypes.PERL_COMMENT_BLOCK, new TextAttributesKey[] {PERL_COMMENT});

    attributesMap.put(
        PerlElementTypes.PERL_DEREFERENCE, new TextAttributesKey[] {PERL_DEREFERENCE});

    attributesMap.put(PerlElementTypes.PERL_SUB, new TextAttributesKey[] {PERL_SUB});
    attributesMap.put(
        PerlElementTypes.PERL_SUB_PROTOTYPE_TOKEN,
        new TextAttributesKey[] {PERL_SUB_PROTOTYPE_TOKEN});
    attributesMap.put(
        PerlElementTypes.PERL_SUB_ATTRIBUTE, new TextAttributesKey[] {PERL_SUB_ATTRIBUTE});

    attributesMap.put(PerlElementTypes.PERL_HANDLE, new TextAttributesKey[] {PERL_HANDLE});

    attributesMap.put(PerlElementTypes.TEMPLATE_BLOCK_HTML, new TextAttributesKey[] {PERL_COMMENT});
    attributesMap.put(PerlElementTypes.PERL_HEREDOC_END, new TextAttributesKey[] {PERL_SQ_STRING});

    attributesMap.put(
        PerlElementTypes.PERL_STRING_CONTENT, new TextAttributesKey[] {PERL_SQ_STRING});

    attributesMap.put(
        PerlElementTypes.PERL_REGEX_QUOTE_CLOSE, new TextAttributesKey[] {PERL_REGEX_QUOTE});
    attributesMap.put(
        PerlElementTypes.PERL_REGEX_QUOTE_OPEN, new TextAttributesKey[] {PERL_REGEX_QUOTE});
    attributesMap.put(
        PerlElementTypes.PERL_REGEX_TOKEN, new TextAttributesKey[] {PERL_REGEX_TOKEN});
    attributesMap.put(PerlElementTypes.PERL_REGEX_MODIFIER, new TextAttributesKey[] {PERL_KEYWORD});

    attributesMap.put(PerlElementTypes.PERL_QUOTE, new TextAttributesKey[] {PERL_SQ_STRING});
    attributesMap.put(PerlElementTypes.PERL_SEMI, new TextAttributesKey[] {PERL_SEMICOLON});
    attributesMap.put(PerlElementTypes.PERL_LBRACE, new TextAttributesKey[] {PERL_BRACE});
    attributesMap.put(PerlElementTypes.PERL_RBRACE, new TextAttributesKey[] {PERL_BRACE});
    attributesMap.put(PerlElementTypes.PERL_LBRACK, new TextAttributesKey[] {PERL_BRACK});
    attributesMap.put(PerlElementTypes.PERL_RBRACK, new TextAttributesKey[] {PERL_BRACK});
    attributesMap.put(PerlElementTypes.PERL_LPAREN, new TextAttributesKey[] {PERL_PAREN});
    attributesMap.put(PerlElementTypes.PERL_RPAREN, new TextAttributesKey[] {PERL_PAREN});
    attributesMap.put(PerlElementTypes.PERL_LANGLE, new TextAttributesKey[] {PERL_ANGLE});
    attributesMap.put(PerlElementTypes.PERL_RANGLE, new TextAttributesKey[] {PERL_ANGLE});

    attributesMap.put(PerlElementTypes.PERL_NUMBER, new TextAttributesKey[] {PERL_NUMBER});
    attributesMap.put(PerlElementTypes.PERL_NUMBER_VERSION, new TextAttributesKey[] {PERL_VERSION});

    attributesMap.put(PerlElementTypes.PERL_OPERATOR, new TextAttributesKey[] {PERL_OPERATOR});
    attributesMap.put(PerlElementTypes.PERL_OPERATOR_DIV, new TextAttributesKey[] {PERL_OPERATOR});
    attributesMap.put(PerlElementTypes.PERL_OPERATOR_X, new TextAttributesKey[] {PERL_OPERATOR});
    attributesMap.put(
        PerlElementTypes.PERL_OPERATOR_FILETEST, new TextAttributesKey[] {PERL_OPERATOR});
    attributesMap.put(PerlElementTypes.PERL_OPERATOR_NOT, new TextAttributesKey[] {PERL_OPERATOR});
    attributesMap.put(
        PerlElementTypes.PERL_OPERATOR_UNARY, new TextAttributesKey[] {PERL_OPERATOR});

    attributesMap.put(PerlElementTypes.PERL_RESERVED, new TextAttributesKey[] {PERL_KEYWORD});
    attributesMap.put(PerlElementTypes.PERL_TAG, new TextAttributesKey[] {PERL_TAG});

    attributesMap.put(PerlElementTypes.PERL_PACKAGE, new TextAttributesKey[] {PERL_PACKAGE});
    attributesMap.put(PerlElementTypes.PERL_HANDLE, new TextAttributesKey[] {PERL_HANDLE});

    attributesMap.put(PerlElementTypes.PERL_SIGIL_SCALAR, new TextAttributesKey[] {PERL_SCALAR});
    attributesMap.put(
        PerlElementTypes.PERL_SIGIL_SCALAR_INDEX, new TextAttributesKey[] {PERL_SCALAR});
    attributesMap.put(PerlElementTypes.PERL_SIGIL_ARRAY, new TextAttributesKey[] {PERL_ARRAY});
    attributesMap.put(PerlElementTypes.PERL_SIGIL_HASH, new TextAttributesKey[] {PERL_HASH});
    attributesMap.put(
        PerlElementTypes.PERL_VARIABLE_NAME,
        new TextAttributesKey[] {DefaultLanguageHighlighterColors.IDENTIFIER});
  }

  private static final SyntaxHighlighter POD_SYNTAX_HIGHLIGHTER = new PodSyntaxHighlighter();

  @NotNull
  @Override
  public Lexer getHighlightingLexer() {
    return new PerlHighlightningLexer();
  }

  @NotNull
  @Override
  public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {

    TextAttributesKey[] attributesKeys;

    if (tokenType instanceof PodTokenType)
      attributesKeys = POD_SYNTAX_HIGHLIGHTER.getTokenHighlights(tokenType);
    else attributesKeys = attributesMap.get(tokenType);

    return attributesKeys == null ? EMPTY_KEYS : attributesKeys;
  }
}