com.intellij.psi.TokenType Java Examples

The following examples show how to use com.intellij.psi.TokenType. 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: BcfgSyntaxHighlighter.java    From buck with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public TextAttributesKey[] getTokenHighlights(@Nullable IElementType tokenType) {
  if (BcfgTypes.COMMENT.equals(tokenType)) {
    return COMMENT_KEYS;
  } else if (BcfgTypes.L_BRACKET.equals(tokenType)
      || BcfgTypes.R_BRACKET.equals(tokenType)
      || BcfgTypes.REQUIRED_FILE.equals(tokenType)
      || BcfgTypes.OPTIONAL_FILE.equals(tokenType)
      || BcfgTypes.END_INLINE.equals(tokenType)
      || BcfgTypes.ASSIGN.equals(tokenType)) {
    return PUNCTUATION_KEYS;
  } else if (BcfgTypes.PROPERTY_VALUE_FRAGMENT.equals(tokenType)) {
    return VALUE_KEYS;
  } else if (BcfgTypes.SECTION_NAME.equals(tokenType)) {
    return SECTION_KEYS;
  } else if (BcfgTypes.PROPERTY_NAME.equals(tokenType)) {
    return PROPERTY_KEYS;
  } else if (BcfgTypes.FILE_PATH.equals(tokenType)) {
    return FILE_PATH_KEYS;
  } else if (TokenType.BAD_CHARACTER.equals(tokenType)) {
    return BAD_CHAR_KEYS;
  } else {
    return EMPTY_KEYS;
  }
}
 
Example #2
Source File: DefaultASTLeafFactory.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public LeafElement createLeaf(@Nonnull IElementType type, @Nonnull LanguageVersion languageVersion, @Nonnull CharSequence text) {
  final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(type.getLanguage());
  if(parserDefinition != null) {
    if(parserDefinition.getCommentTokens(languageVersion).contains(type)) {
      return new PsiCoreCommentImpl(type, text);
    }
  }

  // this is special case, then type is WHITE_SPACE, but no parser definition
  if(type == TokenType.WHITE_SPACE) {
    return new PsiWhiteSpaceImpl(text);
  }

  if (type instanceof ILeafElementType) {
    return (LeafElement)((ILeafElementType)type).createLeafNode(text);
  }
  return new LeafPsiElement(type, text);
}
 
Example #3
Source File: DustSyntaxHighlighter.java    From Intellij-Dust with MIT License 6 votes vote down vote up
@NotNull
@Override
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
  if (isPartOfComment(tokenType)) {
    return COMMENT_KEYS;
  } else if (tokenType.equals(DustTypes.NUMBER)) {
    return NUMBER_KEYS;
  } else if (tokenType.equals(DustTypes.IDENTIFIER)) {
    return IDENTIFIER_KEYS;
  } else if (isPartOfTag(tokenType)) {
    return TAG_KEYS;
  } else if (tokenType.equals(DustTypes.STRING) || tokenType.equals(DustTypes.STRING_START) || tokenType.equals(DustTypes.STRING_END)) {
    return STRING_KEYS;
  } else if (tokenType.equals(TokenType.BAD_CHARACTER)) {
    return BAD_CHAR_KEYS;
  } else {
    return EMPTY_KEYS;
  }
}
 
Example #4
Source File: GraphQLBlock.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
@Override
protected List<Block> buildChildren() {
    if (blocks == null) {
        blocks = ContainerUtil.mapNotNull(myNode.getChildren(null), node -> {
            if(node.getTextLength() == 0) {
                return null;
            }
            if(node.getElementType() == TokenType.WHITE_SPACE) {
                if(node.getText().contains(",")) {
                    // non-significant comma, but not empty text according to IDEA
                    return new GraphQLBlock(node, null, null);
                }
                return null;
            }
            return new GraphQLBlock(node, null, null);
        });
    }
    return blocks;
}
 
Example #5
Source File: ProtoFoldingBuilder.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
private static PsiElement findFurthestSiblingOfSameType(@NotNull PsiElement anchor, boolean after) {
    ASTNode node = anchor.getNode();
    final IElementType expectedType = node.getElementType();
    ASTNode lastSeen = node;
    while (node != null) {
        final IElementType elementType = node.getElementType();
        if (elementType == expectedType) {
            lastSeen = node;
        } else if (elementType == TokenType.WHITE_SPACE) {
            if (expectedType == token(LINE_COMMENT)
                    && node.getText().indexOf('\n', 1) != -1) {
                break;
            }
        } else if (!COMMENT_TOKEN_SET.contains(elementType) || COMMENT_TOKEN_SET.contains(expectedType)) {
            break;
        }
        node = after ? node.getTreeNext() : node.getTreePrev();
    }
    return lastSeen.getPsi();
}
 
Example #6
Source File: ConceptSyntaxHighlighter.java    From Intellij-Plugin with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
    if (tokenType.equals(ConceptTokenTypes.CONCEPT_HEADING)) {
        return HighlighterTokens.SPEC_HEADING_ATTRIBUTE;
    } else if (tokenType.equals(ConceptTokenTypes.STEP)) {
        return HighlighterTokens.STEP_ATTRIBUTE;
    } else if (tokenType.equals(ConceptTokenTypes.COMMENT)) {
        return HighlighterTokens.COMMENT_ATTRIBUTE;
    } else if (tokenType.equals(ConceptTokenTypes.TABLE_HEADER)) {
        return HighlighterTokens.TABLE_HEADER_ATTRIBUTE;
    } else if (tokenType.equals(ConceptTokenTypes.TABLE_ROW_VALUE)) {
        return HighlighterTokens.TABLE_ROW_ATTRIBUTE;
    } else if (tokenType.equals(ConceptTokenTypes.TABLE_BORDER)) {
        return HighlighterTokens.TABLE_BORDER_ATTRIBUTE;
    } else if (tokenType.equals(ConceptTokenTypes.ARG_START) || tokenType.equals(ConceptTokenTypes.ARG) || tokenType.equals(ConceptTokenTypes.ARG_END)) {
        return HighlighterTokens.ARG_ATTRIBUTE;
    } else if (tokenType.equals(ConceptTokenTypes.DYNAMIC_ARG_START) || tokenType.equals(ConceptTokenTypes.DYNAMIC_ARG) || tokenType.equals(ConceptTokenTypes.DYNAMIC_ARG_END)) {
        return HighlighterTokens.DYNAMIC_ARG_ATTRIBUTE;
    } else if (tokenType.equals(TokenType.BAD_CHARACTER)) {
        return HighlighterTokens.BAD_CHAR_KEYS;
    } else {
        return HighlighterTokens.EMPTY_KEYS;
    }
}
 
Example #7
Source File: LatteBlock.java    From intellij-latte with MIT License 6 votes vote down vote up
@NotNull
@Override
protected Indent getChildIndent(@NotNull ASTNode astNode) {
	if (isBellowType(astNode, LatteTypes.MACRO_CONTENT)
		&& astNode.getTreePrev() != null
		&& astNode.getTreePrev().getElementType() == TokenType.WHITE_SPACE) {
		return Indent.getNormalIndent();
	}
	if (!isPair || isOpening(astNode) || isClosing(astNode)) {
		return Indent.getNoneIndent();
	}
	if (!(astNode.getPsi() instanceof LatteMacroClassic)) {
		return Indent.getNormalIndent();
	}
	PsiElement el = astNode.getPsi();
	LatteMacroTag openTag = ((LatteMacroClassic) el).getOpenTag();
	if (openTag.matchMacroName("else") || openTag.matchMacroName("elseif") || openTag.matchMacroName("elseifset")) {
		return Indent.getNoneIndent();
	}
	return Indent.getNormalIndent();
}
 
Example #8
Source File: JSGraphQLEndpointBlock.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
@Override
protected List<Block> buildChildren() {

	List<Block> blocks = new ArrayList<>();
	ASTNode child = myNode.getFirstChildNode();
	while (child != null) {
		if (!TokenType.WHITE_SPACE.equals(child.getElementType()) && !TokenType.BAD_CHARACTER.equals(child.getElementType())) {
			if (!child.getTextRange().isEmpty()) {
				JSGraphQLEndpointBlock block = new JSGraphQLEndpointBlock(this, child, null, null, spacingBuilder);
				blocks.add(block);
			}
		}
		child = child.getTreeNext();
	}

	return blocks;
}
 
Example #9
Source File: SimpleSyntaxHighlighter.java    From intellij-sdk-docs with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
  if (tokenType.equals(SimpleTypes.SEPARATOR)) {
    return SEPARATOR_KEYS;
  } else if (tokenType.equals(SimpleTypes.KEY)) {
    return KEY_KEYS;
  } else if (tokenType.equals(SimpleTypes.VALUE)) {
    return VALUE_KEYS;
  } else if (tokenType.equals(SimpleTypes.COMMENT)) {
    return COMMENT_KEYS;
  } else if (tokenType.equals(TokenType.BAD_CHARACTER)) {
    return BAD_CHAR_KEYS;
  } else {
    return EMPTY_KEYS;
  }
}
 
Example #10
Source File: PrefixSuffixAddingLexerTest.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@Test
public void testIssue333() throws Exception {
    PrefixSuffixAddingLexer lexer = new PrefixSuffixAddingLexer(new BashLexer(BashVersion.Bash_v4), "\"", TokenType.WHITE_SPACE, "\"", TokenType.WHITE_SPACE);
    //Lexer lexer = new BashLexer(BashVersion.Bash_v4);
    lexer.start("\"x$a\"");

    Assert.assertEquals(TokenType.WHITE_SPACE, lexer.getTokenType());
    Assert.assertEquals("\"", lexer.getTokenText());

    lexer.advance();
    Assert.assertEquals(BashTokenTypes.WORD, lexer.getTokenType());
    Assert.assertEquals("x", lexer.getTokenText());

    lexer.advance();
    Assert.assertEquals(BashTokenTypes.VARIABLE, lexer.getTokenType());
    Assert.assertEquals("$a", lexer.getTokenText());

    lexer.advance();
    Assert.assertEquals(BashTokenTypes.WHITESPACE, lexer.getTokenType());
    Assert.assertEquals("\"", lexer.getTokenText());
}
 
Example #11
Source File: BuckBlock.java    From Buck-IntelliJ-Plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Recursively build sub blocks
 */
private List<BuckBlock> buildSubBlocks() {
  final List<BuckBlock> blocks = new ArrayList<BuckBlock>();
  for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) {
    final IElementType childType = child.getElementType();

    if (child.getTextRange().isEmpty()) {
      continue;
    }
    if (childType == TokenType.WHITE_SPACE) {
      continue;
    }
    // In most cases, glob block doesn't need reformatting. So just ignore it for now.
    if (childType == BuckTypes.GLOB_BLOCK) {
      continue;
    }
    blocks.add(buildSubBlock(child));
  }
  return Collections.unmodifiableList(blocks);
}
 
Example #12
Source File: CsvValidationInspection.java    From intellij-csv-validator with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PsiElementVisitor() {
        @Override
        public void visitElement(PsiElement element) {
            if (element == null || !holder.getFile().getLanguage().isKindOf(CsvLanguage.INSTANCE)) {
                return;
            }

            IElementType elementType = CsvHelper.getElementType(element);
            PsiElement firstChild = element.getFirstChild();
            PsiElement nextSibling = element.getNextSibling();
            if (elementType == TokenType.ERROR_ELEMENT && firstChild != null && element.getText().equals(firstChild.getText())) {
                CsvValidationInspection.this.registerError(holder, element, UNESCAPED_SEQUENCE, fixUnescapedSequence);
                if (!"\"".equals(firstChild.getText())) {
                    CsvValidationInspection.this.registerError(holder, element, SEPARATOR_MISSING, fixSeparatorMissing);
                }
            } else if ((elementType == CsvTypes.TEXT || elementType == CsvTypes.ESCAPED_TEXT) &&
                    CsvHelper.getElementType(nextSibling) == TokenType.ERROR_ELEMENT &&
                    nextSibling.getFirstChild() == null) {
                CsvValidationInspection.this.registerError(holder, element, CLOSING_QUOTE_MISSING, fixClosingQuoteMissing);
            }
        }
    };
}
 
Example #13
Source File: HXMLSyntaxHighlighter.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
  if (tokenType.equals(HXMLTypes.KEY_TOKEN)) {
    return KEY_KEYS;
  }
  else if (tokenType.equals(HXMLTypes.QUALIFIEDCLASSNAME)) {
    return CLASS_NAME_KEYS;
  }
  else if (tokenType.equals(HXMLTypes.VALUE_TOKEN)) {
    return VALUE_KEYS;
  }
  else if (tokenType.equals(HXMLTypes.COMMENT)) {
    return COMMENT_KEYS;
  }
  else if (tokenType.equals(TokenType.BAD_CHARACTER)) {
    return BAD_CHAR_KEYS;
  }
  if (tokenType.equals(HXMLTypes.HXML_FILE)) {
    return INCLUDE_KEYS;
  }
  else {
    return EMPTY_KEYS;
  }
}
 
Example #14
Source File: CsvSyntaxHighlighter.java    From intellij-csv-validator with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
    if (tokenType.equals(CsvTypes.COMMA)) {
        return COMMA_KEYS;
    } else if (tokenType.equals(CsvTypes.QUOTE)) {
        return QUOTE_KEYS;
    } else if (tokenType.equals(CsvTypes.TEXT)) {
        return TEXT_KEYS;
    } else if (tokenType.equals(CsvTypes.ESCAPED_TEXT)) {
        return ESCAPED_TEXT_KEYS;
    } else if (tokenType.equals(TokenType.BAD_CHARACTER)) {
        return BAD_CHAR_KEYS;
    } else {
        return EMPTY_KEYS;
    }
}
 
Example #15
Source File: DotEnvSyntaxHighlighter.java    From idea-php-dotenv-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
    if (tokenType.equals(DotEnvTypes.SEPARATOR)) {
        return SEPARATOR_KEYS;
    } else if (tokenType.equals(DotEnvTypes.KEY_CHARS)) {
        return KEY_KEYS;
    } else if (tokenType.equals(DotEnvTypes.VALUE_CHARS)) {
        return VALUE_KEYS;
    } else if (tokenType.equals(DotEnvTypes.COMMENT) || tokenType.equals(DotEnvTypes.EXPORT)) {
        return COMMENT_KEYS;
    } else if (tokenType.equals(TokenType.BAD_CHARACTER)) {
        return BAD_CHAR_KEYS;
    } else {
        return EMPTY_KEYS;
    }
}
 
Example #16
Source File: SoyLexer.java    From bamboo-soy with Apache License 2.0 6 votes vote down vote up
@Override
public MergeFunction getMergeFunction() {
  return ((final IElementType type, final Lexer originalLexer) -> {
    if (type == SoyTypes.OTHER || type == TokenType.WHITE_SPACE) {
      IElementType returnType =  type;
      while (originalLexer.getTokenType() == SoyTypes.OTHER
          || originalLexer.getTokenType() == TokenType.WHITE_SPACE) {
        if (originalLexer.getTokenType() == SoyTypes.OTHER) {
          returnType = SoyTypes.OTHER;
        }
        originalLexer.advance();
      }
      return returnType;
    }

    return type;
  });
}
 
Example #17
Source File: FormatterUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static ASTNode getWsCandidate(@Nullable ASTNode node) {
  if (node == null) return null;
  ASTNode treePrev = node.getTreePrev();
  if (treePrev != null) {
    if (treePrev.getElementType() == TokenType.WHITE_SPACE) {
      return treePrev;
    }
    else if (treePrev.getTextLength() == 0) {
      return getWsCandidate(treePrev);
    }
    else {
      return node;
    }
  }
  final ASTNode treeParent = node.getTreeParent();

  if (treeParent == null || treeParent.getTreeParent() == null) {
    return node;
  }
  else {
    return getWsCandidate(treeParent);
  }
}
 
Example #18
Source File: TypoScriptSyntaxHighlighter.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
    if (tokenType.equals(TypoScriptTypes.DOT)) {
        return SEPARATOR_KEYS;
    } else if (tokenType.equals(TypoScriptTypes.IDENTIFIER)) {
        return KEY_KEYS;
    } else if (tokenType.equals(TypoScriptTypes.VALUE)) {
        return VALUE_KEYS;
    } else if (tokenType.equals(TypoScriptTypes.COMMENT)) {
        return COMMENT_KEYS;
    } else if (tokenType.equals(TokenType.BAD_CHARACTER)) {
        return BAD_CHAR_KEYS;
    } else {
        return EMPTY_KEYS;
    }
}
 
Example #19
Source File: FluidFileHighlighter.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@NotNull
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
    if (tokenType.equals(FluidTypes.FIELD_CHAIN) || tokenType.equals(FluidTypes.IDENTIFIER) || tokenType.equals(FluidTypes.NAMESPACE)) {
        return IDENTIFIER_KEYS;
    } else if (tokenType.equals(FluidTypes.EXPR_END) || tokenType.equals(FluidTypes.EXPR_START)) {
        return BRACES_KEYS;
    } else if (tokenType.equals(FluidTypes.TEMPLATE_COMMENT)) {
        return COMMENT_KEYS;
    } else if (tokenType.equals(FluidTypes.STRING_LITERAL) || tokenType.equals(FluidTypes.DOUBLE_QUOTED_STRING) || tokenType.equals(FluidTypes.SINGLE_QUOTED_STRING)) {
        return STRING_KEYS;
    } else if (tokenType.equals(FluidTypes.ARROW)) {
        return OPERATION_KEYS;
    } else if (tokenType.equals(TokenType.BAD_CHARACTER)) {
        return BAD_CHAR_KEYS;
    } else {
        return EMPTY_KEYS;
    }
}
 
Example #20
Source File: LombokConfigSyntaxHighlighter.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@NotNull
@Override
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
  if (tokenType.equals(LombokConfigTypes.SEPARATOR) || tokenType.equals(LombokConfigTypes.SIGN)) {
    return SEPARATOR_KEYS;
  } else if (tokenType.equals(LombokConfigTypes.CLEAR)) {
    return CLEAR_KEYS;
  } else if (tokenType.equals(LombokConfigTypes.KEY)) {
    return KEY_KEYS;
  } else if (tokenType.equals(LombokConfigTypes.VALUE)) {
    return VALUE_KEYS;
  } else if (tokenType.equals(LombokConfigTypes.COMMENT)) {
    return COMMENT_KEYS;
  } else if (tokenType.equals(TokenType.BAD_CHARACTER)) {
    return BAD_CHAR_KEYS;
  } else {
    return EMPTY_KEYS;
  }
}
 
Example #21
Source File: PsiBuilderQuickTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public IElementType getTokenType() {
  if (myIndex >= myBufferEnd) {
    return null;
  }
  else if (Character.isLetter(myBuffer.charAt(myIndex))) {
    return LETTER;
  }
  else if (Character.isDigit(myBuffer.charAt(myIndex))) {
    return DIGIT;
  }
  else if (Character.isWhitespace(myBuffer.charAt(myIndex))) {
    return TokenType.WHITE_SPACE;
  }
  else if (myBuffer.charAt(myIndex) == '#') {
    return COMMENT;
  }
  else {
    return OTHER;
  }
}
 
Example #22
Source File: XQueryFormattingBlock.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Block> buildChildren() {
    List<Block> blocks = new ArrayList<Block>();
    ASTNode child = myNode.getFirstChildNode();
    while (child != null) {
        if (child.getElementType() != TokenType.WHITE_SPACE && child.getTextRange().getLength() != 0) {
            Block block = new XQueryFormattingBlock(child, Wrap.createWrap(WrapType.NONE, false), null, settings,
                    spacingBuilder);
            blocks.add(block);
        }
        child = child.getTreeNext();
    }
    return blocks;
}
 
Example #23
Source File: UsefulPsiTreeUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
public static ASTNode[] findChildrenRange(ASTNode[] elements, int startOffset, int endOffset)
{
	int i = findChildIndex(elements, startOffset);
	int j = findChildIndex(elements, endOffset);
	i = i == -1 ? 0 : i;
	j = j == -1 ? elements.length : j;
	// trim
	while(0 < j && j < elements.length && elements[j].getElementType() == TokenType.WHITE_SPACE)
	{
		--j;
	}
	int to = j;
	if(j < elements.length && elements[j].getElementType() != CSharpTokens.SEMICOLON)
	{
		// try eat until ';'
		while(j + 1 < elements.length && (elements[j + 1].getElementType() == CSharpTokens.SEMICOLON || elements[j + 1].getElementType() ==
				TokenType.WHITE_SPACE))
		{
			++j;
			if(elements[j].getElementType() == CSharpTokens.SEMICOLON)
			{
				to = j;
			}
		}
	}
	to = Math.min(elements.length, to + 1);
	if(to < i)
	{
		return ASTNode.EMPTY_ARRAY;
	}
	return Arrays.copyOfRange(elements, i, to);
}
 
Example #24
Source File: TokenSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new token set containing the specified element types.
 *
 * @param types the element types contained in the set.
 * @return the new token set.
 */
@Nonnull
public static TokenSet create(@Nonnull IElementType... types) {
  if (types.length == 0) return EMPTY;
  if (types.length == 1 && types[0] == TokenType.WHITE_SPACE) {
    return WHITE_SPACE;
  }
  return doCreate(types);
}
 
Example #25
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 #26
Source File: HaxeConditionalCompilationLexerSupport.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
public static Type deriveBlockType(@Nullable IElementType el) {
  if (null == el || el == TokenType.DUMMY_HOLDER) {
    return Type.ROOT;
  }
  if (el == PPIF)          { return Type.IF; }
  else if (el == PPELSEIF) { return Type.ELSEIF; }
  else if (el == PPELSE)   { return Type.ELSE; }
  else if (el == PPEND)    { return Type.END; }
  else {
    LOG.debug("Unrecognized compiler conditional is being used.");
  }
  return Type.UNKNOWN;
}
 
Example #27
Source File: PsiBuilderImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static CompositeElement createComposite(@Nonnull StartMarker marker) {
  final IElementType type = marker.myType;
  if (type == TokenType.ERROR_ELEMENT) {
    String message = marker.myDoneMarker instanceof DoneWithErrorMarker ? ((DoneWithErrorMarker)marker.myDoneMarker).myMessage : null;
    return Factory.createErrorElement(message);
  }

  if (type == null) {
    throw new RuntimeException(UNBALANCED_MESSAGE);
  }

  return ASTFactory.composite(type);
}
 
Example #28
Source File: HaskellFoldingBuilder.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
private static void
addCommentFolds(@NotNull PsiComment c,
                @NotNull Set<PsiElement> processedComments,
                @NotNull List<FoldingDescriptor> foldElements) {
    if (processedComments.contains(c) ||
            !HaskellTypes.COMMENT.equals(c.getTokenType())) {
        return;
    }

    PsiElement end = null;
    for (PsiElement curr = c.getNextSibling(); curr != null; curr = curr.getNextSibling()) {
        ASTNode node = curr.getNode();
        if (node == null) {
            break;
        }
        IElementType elementType = node.getElementType();
        if (HaskellTypes.COMMENT.equals(elementType)) {
            end = curr;
            // We don't want to process, say, the second comment in case of
            // three subsequent comments when it's being examined during all
            // elements traversal. I.e. we expect to start from the first
            // comment and grab as many subsequent comments as possible
            // during the single iteration.
            processedComments.add(curr);
            continue;
        }
        if (TokenType.WHITE_SPACE.equals(elementType)) {
            continue;
        }
        break;
    }

    if (end != null) {
        TextRange rng = new TextRange(c.getTextRange().getStartOffset(),
                                      end.getTextRange().getEndOffset());
        foldElements.add(new FoldingDescriptor(c, rng));
    }
}
 
Example #29
Source File: BuckCopyPasteProcessor.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Converts raw paste text to a format suitable for insertion in a buck dependency list (e.g.,
 * {@code deps} or {@code visibility} at the point of the given element. The paste text may span
 * multiple lines. If every non-blank line can be resolved into a dependency, the expanded text is
 * returned, but if any non-blank line in unresolvable, the original paste text is returned.
 *
 * <p>This method detects the location in the parse tree from the given element and will
 * quote-wrap dependencies accordingly.
 *
 * <p>Example resolutions (note that these are the unwrapped results; wrapped results are
 * double-quoted with a trailing comma):
 *
 * <p>Java imports: {@code "import com.example.activity.MyFirstActivity" ->
 * "//java/com/example/activity:activity" }
 *
 * <p>Java packages: {@code "package com.example.activity;" ->
 * "//java/com/example/activity:activity" }
 *
 * <p>Fully qualified Java classnames: {@code "com.example.activity.MyFirstActivity" ->
 * "//java/com/example/activity:activity" }
 *
 * <p>Unqualified Java classnames: {@code "MyFirstActivity" ->
 * "//java/com/example/activity:activity" } (when there is a unique match for the classname)
 *
 * <p>BUCK paths: {@code "//java/com/example/activity/BUCK" ->
 * "//java/com/example/activity:activity" }
 *
 * <p>BUCK targets: {@code "//java/com/example/activity:activity" ->
 * "//java/com/example/activity:activity" }
 *
 * <p>Multiline pastes: {@code "import com.foo.Foo;\nimport com.bar.Bar;" ->
 * "//java/com/foo:foo\n//java/com/bar:bar"}
 */
private String formatPasteText(
    String text, PsiElement element, Project project, boolean inQuotedString) {
  Iterable<String> paths = Splitter.on('\n').trimResults().omitEmptyStrings().split(text);
  List<String> results = new ArrayList<>();
  for (String path : paths) {
    String resolution = null;

    Matcher matcher = UNSOLVED_DEPENDENCY_PATTERN.matcher(path);
    if (matcher.matches()) {
      resolution = resolveUnsolvedBuckDependency(project, matcher.group(2));
    } else if (SOLVED_DEPENDENCY_PATTERN.matcher(path).matches()) {
      resolution = buildSolvedBuckDependency(path);
    } // else we don't know how to format this

    if (resolution == null) {
      // Any non-target results in no formatting
      return text;
    }

    // We have text to paste - figure out if we should wrap it in "\"%s\","
    IElementType elementType = element.getNode().getElementType();

    // Is the cursor in whitespace?
    boolean whitespace = elementType == TokenType.WHITE_SPACE;

    // If the cursor is in whitespace, or under a left quote, then we should wrap the paste text
    if (whitespace || !inQuotedString) {
      resolution = "\"" + resolution + "\",";
    }

    results.add(resolution);
  }
  return Joiner.on('\n').skipNulls().join(results);
}
 
Example #30
Source File: SimpleBlock.java    From intellij-sdk-docs with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Block> buildChildren() {
  List<Block> blocks = new ArrayList<>();
  ASTNode child = myNode.getFirstChildNode();
  while (child != null) {
    if (child.getElementType() != TokenType.WHITE_SPACE) {
      Block block = new SimpleBlock(child, Wrap.createWrap(WrapType.NONE, false), Alignment.createAlignment(),
                                    spacingBuilder);
      blocks.add(block);
    }
    child = child.getTreeNext();
  }
  return blocks;
}