Java Code Examples for com.intellij.psi.TokenType#WHITE_SPACE

The following examples show how to use com.intellij.psi.TokenType#WHITE_SPACE . 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: 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 2
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 3
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 4
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 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: 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;
}
 
Example 7
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 8
Source File: BuckCopyPasteProcessor.java    From Buck-IntelliJ-Plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Automatically convert to buck dependency pattern
 * Example 1:
 * "import com.example.activity.MyFirstActivity" -> "//java/com/example/activity:activity"
 *
 * Example 2:
 * "package com.example.activity;" -> "//java/com/example/activity:activity"
 *
 * Example 3:
 * "com.example.activity.MyFirstActivity" -> "//java/com/example/activity:activity"
 *
 * Example 4:
 * "/Users/tim/tb/java/com/example/activity/BUCK" -> "//java/com/example/activity:activity"
 */
private String buildBuckDependencyPath(PsiElement element, Project project, String path) {
  String original = path;
  Matcher matcher = DEPENDENCY_PATTERN.matcher(path);
  if (matcher.matches()) {
    path = matcher.group(2);
  }

  VirtualFile buckFile = referenceNameToBuckFile(project, path);
  if (buckFile != null) {
    path = buckFile.getPath().replaceFirst(project.getBasePath(), "");
    path = "/" + path.replace('.', '/');
    path = path.substring(0, path.lastIndexOf("/"));

    String target = BuckBuildUtil.extractBuckTarget(project, buckFile);
    if (target != null) {
      path += target;
    } else {
      String lastWord = path.substring(path.lastIndexOf("/") + 1, path.length());
      path += ":" + lastWord;
    }
    if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
      path = "'" + path + "',";
    }
    return path;
  } else {
    return original;
  }
}
 
Example 9
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 10
Source File: FusionBlock.java    From intellij-neos with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @return An ArrayList of all child elements of this block.
 */
private List<Block> buildSubBlocks() {
    List<Block> blocks = new ArrayList<Block>();
    for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) {
        if (child.getElementType() == FusionTypes.VALUE_DSL_CONTENT) {
            // This is a special case to handle auto-formatting of AFX code (which is implemented as
            // Language Injection; see fusionInjections.xml):
            //
            // We try to delegate block creation to the nested language (i.e. HTML in case of AFX). If this
            // is successful (`addInjectedBlocks` returns TRUE), we skip creating a block for this part on our own.
            // In this case, the blocks covering the afx`...` part of the code are from the embedded language. Thus,
            // autoformatting works properly for AFX code.
            //
            // a hint to this method has been found via https://intellij-support.jetbrains.com/hc/en-us/community/posts/206749635-Injected-languages-formatting
            if (injectedLanguageBlockBuilder.addInjectedBlocks(blocks, child, getWrap(), getAlignment(), Indent.getNormalIndent())) {
                continue;
            }
        }


        IElementType childType = child.getElementType();
        if (child.getTextRange().getLength() == 0 || childType == TokenType.WHITE_SPACE ||
                childType == FusionTypes.CRLF
                ) {
            continue;
        }
        blocks.add(new FusionBlock(child, null, null, MY_SETTINGS, MY_SPACING_BUILDER, injectedLanguageBlockBuilder));
    }
    return Collections.unmodifiableList(blocks);
}
 
Example 11
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 12
Source File: CsvBlockField.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Block> buildChildren() {
    ASTNode node = this.getNode().getFirstChildNode();
    List<Block> blocks = new ArrayList<>();
    while (node != null) {
        if (node.getElementType() != TokenType.WHITE_SPACE) {
            CsvBlockElement block = new CsvBlockElement(node, myFormattingInfo, this);
            blocks.add(block);
        }
        node = node.getTreeNext();
    }
    return blocks;
}
 
Example 13
Source File: CsvBlock.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Block> buildChildren() {
    List<ASTNode> todoNodes = new ArrayList<>();
    List<Block> blocks = new ArrayList<>();
    todoNodes.add(getNode().getFirstChildNode());
    CsvBlockField currentField = null;
    while (todoNodes.size() > 0) {
        ASTNode node = todoNodes.remove(todoNodes.size() - 1);
        if (node == null) {
            continue;
        }

        IElementType elementType = node.getElementType();
        todoNodes.add(node.getTreeNext());
        if (elementType == CsvTypes.RECORD) {
            todoNodes.add(node.getFirstChildNode());
        } else if (elementType == CsvTypes.FIELD) {
            currentField = new CsvBlockField(node, myFormattingInfo);
            if (currentField.getTextLength() > 0) {
                blocks.add(currentField);
            }
        } else if (elementType == CsvTypes.COMMA || elementType == CsvTypes.CRLF) {
            blocks.add(new CsvBlockElement(node, myFormattingInfo, currentField));
        } else if (elementType != TokenType.WHITE_SPACE && node.getTextLength() > 0) {
            blocks.add(new CsvDummyBlock(node, myFormattingInfo));
        }
    }
    validateBlocks(blocks);
    return blocks;
}
 
Example 14
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 15
Source File: EnterAfterUnmatchedBraceHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Current handler inserts closing curly brace (right brace) if necessary. There is a possible case that it should be located
 * more than one line forward.
 * <p/>
 * <b>Example</b>
 * <pre>
 *     if (test1()) {
 *     } else {<caret> if (test2()) {
 *         foo();
 *     }
 * </pre>
 * <p/>
 * We want to get this after the processing:
 * <pre>
 *     if (test1()) {
 *     } else {
 *         if (test2()) {
 *             foo();
 *         }
 *     }
 * </pre>
 * I.e. closing brace should be inserted two lines below current caret line. Hence, we need to calculate correct offset
 * to use for brace inserting. This method is responsible for that.
 * <p/>
 * In essence it inspects PSI structure and finds PSE elements with the max length that starts at caret offset. End offset
 * of that element is used as an insertion point.
 *
 * @param file   target PSI file
 * @param text   text from the given file
 * @param offset target offset where line feed will be inserted
 * @return pair of (element, offset). The element is the '}' owner, if applicable; the offset is the position for inserting closing brace
 */
protected Pair<PsiElement, Integer> calculateOffsetToInsertClosingBrace(@Nonnull PsiFile file, @Nonnull CharSequence text, final int offset) {
  PsiElement element = PsiUtilCore.getElementAtOffset(file, offset);
  ASTNode node = element.getNode();
  if (node != null && node.getElementType() == TokenType.WHITE_SPACE) {
    return Pair.create(null, CharArrayUtil.shiftForwardUntil(text, offset, "\n"));
  }
  for (PsiElement parent = element.getParent(); parent != null; parent = parent.getParent()) {
    ASTNode parentNode = parent.getNode();
    if (parentNode == null || parentNode.getStartOffset() != offset) {
      break;
    }
    element = parent;
  }
  if (element.getTextOffset() != offset) {
    return Pair.create(null, CharArrayUtil.shiftForwardUntil(text, offset, "\n"));
  }
  return Pair.create(element, calculateOffsetToInsertClosingBraceInsideElement(element));
}
 
Example 16
Source File: CSharpDocBraceMatcher.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
protected boolean isWhitespace(final IElementType tokenType1)
{
	return tokenType1 == TokenType.WHITE_SPACE;
}
 
Example 17
Source File: ElmPairedBraceMatcher.java    From elm-plugin with MIT License 4 votes vote down vote up
@Override
public boolean isPairedBracesAllowedBeforeType(@NotNull IElementType lbraceType, @Nullable IElementType contextType) {
    return contextType == null
            || contextType == TokenType.WHITE_SPACE
            || contextType == ElmTypes.FRESH_LINE;
}
 
Example 18
Source File: CypherBlock.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 4 votes vote down vote up
private static boolean isWhitespaceOrEmpty(ASTNode node) {
    return node.getElementType() == TokenType.WHITE_SPACE || node.getTextLength() == 0;
}
 
Example 19
Source File: XQueryXmlSlashTypedHandler.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
protected ASTNode getPreviousNonWhiteSpaceLeaf(ASTNode originalPrevLeaf) {
    ASTNode prevLeaf = originalPrevLeaf;
    while ((prevLeaf = TreeUtil.prevLeaf(prevLeaf)) != null && prevLeaf.getElementType() == TokenType.WHITE_SPACE) {
    }
    return prevLeaf;
}
 
Example 20
Source File: PsiWhiteSpaceImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
public PsiWhiteSpaceImpl(CharSequence text) {
  super(TokenType.WHITE_SPACE, text);
}