List of usage examples for org.antlr.v4.runtime Token getLine
int getLine();
From source file:org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy.java
License:Open Source License
private Token getFirstTokenOfCursorLine(Parser recognizer) { TokenStream tokenStream = recognizer.getInputStream(); Token firstCursorLineToken = null;//from w w w .j a v a 2 s .c om int cursorLine = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine() + 1; int cursorCol = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter() + 1; int index = 1; Token beforeCursorToken = tokenStream.LT(index); int type = beforeCursorToken.getType(); int tLine = beforeCursorToken.getLine(); int tCol = beforeCursorToken.getCharPositionInLine(); if (cursorLine < tLine || (cursorLine == tLine && cursorCol <= tCol)) { return null; } firstCursorLineToken = (tLine == cursorLine) ? beforeCursorToken : null; while (type != BallerinaParser.EOF && (tLine <= cursorLine)) { int tokenIndex = beforeCursorToken.getTokenIndex(); if (tLine == cursorLine && (firstCursorLineToken == null || firstCursorLineToken.getTokenIndex() > tokenIndex)) { firstCursorLineToken = beforeCursorToken; } index++; beforeCursorToken = tokenStream.LT(index); type = beforeCursorToken.getType(); tLine = beforeCursorToken.getLine(); } return firstCursorLineToken; }
From source file:org.ballerinalang.langserver.completions.CompletionCustomErrorStrategy.java
License:Open Source License
private Token getLastTerminationToken(TokenStream tokenStream) { Token lastTerminationToken = null;// w w w .j a va 2 s . com Position cursorPosition = this.context.get(DocumentServiceKeys.POSITION_KEY).getPosition(); int cursorLine = cursorPosition.getLine() + 1; int cursorCol = cursorPosition.getCharacter() + 1; int index = 1; Token beforeCursorToken = tokenStream.LT(index); int type = beforeCursorToken.getType(); int tLine = beforeCursorToken.getLine(); int tCol = beforeCursorToken.getCharPositionInLine(); while (type != BallerinaParser.EOF && ((tLine < cursorLine) || (tLine == cursorLine && tCol < cursorCol))) { // Checking for terminating tokens with the type number is inconsistent. Thus, uses string comparisons String tokenText = beforeCursorToken.getText(); if (UtilSymbolKeys.SEMI_COLON_SYMBOL_KEY.equals(tokenText) || UtilSymbolKeys.OPEN_BRACE_KEY.equals(tokenText) || UtilSymbolKeys.CLOSE_BRACE_KEY.equals(tokenText) || UtilSymbolKeys.COMMA_SYMBOL_KEY.equals(tokenText)) { lastTerminationToken = beforeCursorToken; } index++; beforeCursorToken = tokenStream.LT(index); type = beforeCursorToken.getType(); tLine = beforeCursorToken.getLine(); tCol = beforeCursorToken.getCharPositionInLine(); } return lastTerminationToken; }
From source file:org.ballerinalang.langserver.completions.CompletionSubRuleParser.java
License:Open Source License
private static boolean fillAnnotationAttachmentMetaInfo(LSContext ctx) { if (ctx.get(CompletionKeys.TOKEN_STREAM_KEY) == null) { return false; }/*from www. ja v a 2 s . c om*/ TokenStream tokenStream = ctx.get(CompletionKeys.TOKEN_STREAM_KEY); Position cursorPosition = ctx.get(DocumentServiceKeys.POSITION_KEY).getPosition(); Queue<String> fieldsQueue = new LinkedList<>(); ArrayList<String> terminalTokens = new ArrayList<>( Arrays.asList(UtilSymbolKeys.FUNCTION_KEYWORD_KEY, UtilSymbolKeys.SERVICE_KEYWORD_KEY, UtilSymbolKeys.ENDPOINT_KEYWORD_KEY, UtilSymbolKeys.OPEN_PARENTHESES_KEY)); int currentTokenIndex = CommonUtil.getCurrentTokenFromTokenStream(ctx); int startIndex = currentTokenIndex; int line = cursorPosition.getLine(); int col = cursorPosition.getCharacter(); String annotationName; String pkgAlias = ""; while (true) { if (startIndex > tokenStream.size() || CommonUtil.getPreviousDefaultToken(tokenStream, startIndex) == null) { return false; } Token token = CommonUtil.getPreviousDefaultToken(tokenStream, startIndex); if (token == null || terminalTokens.contains(token.getText())) { return false; } if (token.getText().equals(UtilSymbolKeys.ANNOTATION_START_SYMBOL_KEY)) { // Breaks when we meet the first annotation start before the given start index break; } else if (token.getText().equals(UtilSymbolKeys.OPEN_BRACE_KEY)) { startIndex = token.getTokenIndex(); // For each annotation found, capture the package alias if (UtilSymbolKeys.PKG_DELIMITER_KEYWORD .equals(CommonUtil.getNthDefaultTokensToLeft(tokenStream, startIndex, 2).getText())) { pkgAlias = CommonUtil.getNthDefaultTokensToLeft(tokenStream, startIndex, 3).getText(); } } else { startIndex = token.getTokenIndex(); } } String tempTokenString = ""; while (true) { if (startIndex > tokenStream.size()) { return false; } Token token = CommonUtil.getNextDefaultToken(tokenStream, startIndex); int tokenLine = token.getLine() - 1; int tokenCol = token.getCharPositionInLine(); if (terminalTokens.contains(token.getText()) || line < tokenLine || (line == tokenLine - 1 && col - 1 < tokenCol)) { break; } else if (UtilSymbolKeys.OPEN_BRACE_KEY.equals(token.getText())) { fieldsQueue.add(tempTokenString); } else if (UtilSymbolKeys.CLOSE_BRACE_KEY.equals(token.getText())) { fieldsQueue.remove(); } else if (!UtilSymbolKeys.PKG_DELIMITER_KEYWORD.equals(token.getText())) { tempTokenString = token.getText(); } startIndex = token.getTokenIndex(); } annotationName = fieldsQueue.poll(); ctx.put(CompletionKeys.ANNOTATION_ATTACHMENT_META_KEY, new AnnotationAttachmentMetaInfo(annotationName, fieldsQueue, pkgAlias, getNextNodeTypeFlag(tokenStream, currentTokenIndex, ctx))); ctx.put(CompletionKeys.SYMBOL_ENV_NODE_KEY, new BLangAnnotationAttachment()); return true; }
From source file:org.ballerinalang.langserver.completions.resolvers.parsercontext.ParserRuleCallableUnitBodyContextResolver.java
License:Open Source License
private static boolean isCursorBeforeToken(Position cursor, Token token) { int cursorLine = cursor.getLine(); int cursorCol = cursor.getCharacter(); int tokenLine = token.getLine() - 1; int tokenCol = token.getCharPositionInLine(); return cursorLine < tokenLine || (cursorLine == tokenLine && cursorCol <= tokenCol); }
From source file:org.ballerinalang.langserver.completions.TreeVisitor.java
License:Open Source License
/** * Check whether the cursor resides within the given node type's parameter context. * Node name is used to identify the correct node * //from w w w .j av a 2 s. c o m * @param nodeName Name of the node * @param nodeType Node type (Function, Resource, Action or Connector) * @return {@link Boolean} Whether the cursor is within the parameter context */ private boolean isWithinParameterContext(String nodeName, String nodeType, SymbolEnv env) { ParserRuleContext parserRuleContext = lsContext.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY); TokenStream tokenStream = lsContext.get(CompletionKeys.TOKEN_STREAM_KEY); String terminalToken = ""; // If the parser rule context is not parameter context or parameter list context, we skipp the calculation if (!(parserRuleContext instanceof BallerinaParser.ParameterContext || parserRuleContext instanceof BallerinaParser.ParameterListContext)) { return false; } int startTokenIndex = parserRuleContext.getStart().getTokenIndex(); ArrayList<String> terminalKeywords = new ArrayList<>( Arrays.asList(UtilSymbolKeys.ACTION_KEYWORD_KEY, UtilSymbolKeys.CONNECTOR_KEYWORD_KEY, UtilSymbolKeys.FUNCTION_KEYWORD_KEY, UtilSymbolKeys.RESOURCE_KEYWORD_KEY)); ArrayList<Token> filteredTokens = new ArrayList<>(); Token openBracket = null; boolean isWithinParams = false; // Find the index of the closing bracket while (true) { if (startTokenIndex > tokenStream.size()) { // In the ideal case, should not reach this point startTokenIndex = -1; break; } Token token = tokenStream.get(startTokenIndex); String tokenString = token.getText(); if (tokenString.equals(")")) { break; } startTokenIndex++; } // Backtrack the token stream to find a terminal token while (true) { if (startTokenIndex < 0) { break; } Token token = tokenStream.get(startTokenIndex); String tokenString = token.getText(); if (terminalKeywords.contains(tokenString)) { terminalToken = tokenString; break; } if (token.getChannel() == Token.DEFAULT_CHANNEL) { filteredTokens.add(token); } startTokenIndex--; } Collections.reverse(filteredTokens); /* This particular logic identifies a matching pair of closing and opening bracket and then check whether the cursor is within those bracket pair */ if (nodeName.equals(filteredTokens.get(0).getText()) && terminalToken.equals(nodeType)) { String tokenText; for (Token token : filteredTokens) { tokenText = token.getText(); if (tokenText.equals("(")) { openBracket = token; } else if (tokenText.equals(")") && openBracket != null) { Position cursorPos = lsContext.get(DocumentServiceKeys.POSITION_KEY).getPosition(); int openBLine = openBracket.getLine() - 1; int openBCol = openBracket.getCharPositionInLine(); int closeBLine = token.getLine() - 1; int closeBCol = token.getCharPositionInLine(); int cursorLine = cursorPos.getLine(); int cursorCol = cursorPos.getCharacter(); isWithinParams = (cursorLine > openBLine && cursorLine < closeBLine) || (cursorLine == openBLine && cursorCol > openBCol && cursorLine < closeBLine) || (cursorLine > openBLine && cursorCol < closeBCol && cursorLine == closeBLine) || (cursorLine == openBLine && cursorLine == closeBLine && cursorCol >= openBCol && cursorCol <= closeBCol); if (isWithinParams) { break; } else { openBracket = null; } } } } if (isWithinParams) { this.populateSymbols(this.resolveAllVisibleSymbols(env), env); forceTerminateVisitor(); } return isWithinParams; }
From source file:org.ballerinalang.langserver.completions.util.CompletionVisitorUtil.java
License:Open Source License
/** * Check whether the cursor resides within the given node type's parameter context. * Node name is used to identify the correct node * * @param nodeName Name of the node * @param nodeType Node type (Function, Resource, Action or Connector) * @param env Symbol Environment * @param lsContext Language Server Operation Context * @param treeVisitor Completion tree visitor instance * @return {@link Boolean} Whether the cursor is within the parameter context *///from w w w . j a v a 2 s . c o m public static boolean isWithinParameterContext(String nodeName, String nodeType, SymbolEnv env, LSContext lsContext, TreeVisitor treeVisitor) { ParserRuleContext parserRuleContext = lsContext.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY); TokenStream tokenStream = lsContext.get(CompletionKeys.TOKEN_STREAM_KEY); String terminalToken = ""; // If the parser rule context is not parameter context or parameter list context, we skipp the calculation if (!(parserRuleContext instanceof BallerinaParser.ParameterContext || parserRuleContext instanceof BallerinaParser.ParameterListContext)) { return false; } int startTokenIndex = parserRuleContext.getStart().getTokenIndex(); ArrayList<String> terminalKeywords = new ArrayList<>( Arrays.asList(UtilSymbolKeys.ACTION_KEYWORD_KEY, UtilSymbolKeys.CONNECTOR_KEYWORD_KEY, UtilSymbolKeys.FUNCTION_KEYWORD_KEY, UtilSymbolKeys.RESOURCE_KEYWORD_KEY)); ArrayList<Token> filteredTokens = new ArrayList<>(); Token openBracket = null; boolean isWithinParams = false; // Find the index of the closing bracket while (true) { if (startTokenIndex > tokenStream.size()) { // In the ideal case, should not reach this point startTokenIndex = -1; break; } Token token = tokenStream.get(startTokenIndex); String tokenString = token.getText(); if (tokenString.equals(")")) { break; } startTokenIndex++; } // Backtrack the token stream to find a terminal token while (true) { if (startTokenIndex < 0) { break; } Token token = tokenStream.get(startTokenIndex); String tokenString = token.getText(); if (terminalKeywords.contains(tokenString)) { terminalToken = tokenString; break; } if (token.getChannel() == Token.DEFAULT_CHANNEL) { filteredTokens.add(token); } startTokenIndex--; } Collections.reverse(filteredTokens); /* This particular logic identifies a matching pair of closing and opening bracket and then check whether the cursor is within those bracket pair */ if (nodeName.equals(filteredTokens.get(0).getText()) && terminalToken.equals(nodeType)) { String tokenText; for (Token token : filteredTokens) { tokenText = token.getText(); if (tokenText.equals("(")) { openBracket = token; } else if (tokenText.equals(")") && openBracket != null) { Position cursorPos = lsContext.get(DocumentServiceKeys.POSITION_KEY).getPosition(); int openBLine = openBracket.getLine() - 1; int openBCol = openBracket.getCharPositionInLine(); int closeBLine = token.getLine() - 1; int closeBCol = token.getCharPositionInLine(); int cursorLine = cursorPos.getLine(); int cursorCol = cursorPos.getCharacter(); isWithinParams = (cursorLine > openBLine && cursorLine < closeBLine) || (cursorLine == openBLine && cursorCol > openBCol && cursorLine < closeBLine) || (cursorLine > openBLine && cursorCol < closeBCol && cursorLine == closeBLine) || (cursorLine == openBLine && cursorLine == closeBLine && cursorCol >= openBCol && cursorCol <= closeBCol); if (isWithinParams) { break; } else { openBracket = null; } } } } if (isWithinParams) { treeVisitor.populateSymbols(treeVisitor.resolveAllVisibleSymbols(env), env); treeVisitor.forceTerminateVisitor(); } return isWithinParams; }
From source file:org.ballerinalang.langserver.completions.util.filters.ConnectorInitExpressionItemFilter.java
License:Open Source License
public List<SymbolInfo> filterItems(TextDocumentServiceContext context) { TokenStream tokenStream = context.get(DocumentServiceKeys.TOKEN_STREAM_KEY); int capturedTokenIndex = context.get(DocumentServiceKeys.TOKEN_INDEX_KEY); // Since the lang server is zero based indexing, we need to increase the line number to align with antlr token int cursorLine = context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine() + 1; int cursorChar = context.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter(); Token capturedToken = tokenStream.get(capturedTokenIndex); Token evaluatorToken = null;//from www.j a va2s . c o m int loopIncrementer = 1; int tokenIterator = capturedTokenIndex; if (capturedToken.getLine() > cursorLine || (capturedToken.getLine() == cursorLine && capturedToken.getCharPositionInLine() >= cursorChar)) { loopIncrementer = -1; } tokenIterator += loopIncrementer; while (true) { if (tokenIterator < 0) { break; } Token tempToken = tokenStream.get(tokenIterator); if (tempToken.getLine() == cursorLine && tempToken.getCharPositionInLine() == cursorChar) { evaluatorToken = getFirstNonHiddenToken(tempToken.getTokenIndex(), -1, tokenStream); break; } tokenIterator += loopIncrementer; } if (evaluatorToken == null) { return new ArrayList<>(); } return getItemsList(evaluatorToken, context); }
From source file:org.ballerinalang.langserver.completions.util.sorters.CallableUnitBodyItemSorter.java
License:Open Source License
@Override public void sortItems(TextDocumentServiceContext ctx, List<CompletionItem> completionItems) { BLangNode previousNode = ctx.get(CompletionKeys.PREVIOUS_NODE_KEY); TokenStream tokenStream = ctx.get(DocumentServiceKeys.TOKEN_STREAM_KEY); if (ctx.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY) != null) { int currentTokenStart = ctx.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY).getStart().getTokenIndex(); Token nextToken = tokenStream.get(currentTokenStart + 1); int cursorLine = ctx.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine(); int cursorChar = ctx.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter(); if (nextToken.getChannel() != Token.DEFAULT_CHANNEL && (cursorLine > nextToken.getLine() - 1 || (cursorLine == nextToken.getLine() - 1 && cursorChar > nextToken.getCharPositionInLine()))) { completionItems.clear();/*from www .ja va 2 s .c om*/ return; } } this.clearItemsIfWorkerExists(ctx, completionItems); if (previousNode == null) { this.populateWhenCursorBeforeOrAfterEp(completionItems); } else if (previousNode instanceof BLangVariableDef) { BType bLangType = ((BLangVariableDef) previousNode).var.type; if (bLangType instanceof BEndpointType) { this.populateWhenCursorBeforeOrAfterEp(completionItems); } else if (ctx.get(CompletionKeys.INVOCATION_STATEMENT_KEY) == null || !ctx.get(CompletionKeys.INVOCATION_STATEMENT_KEY)) { CompletionItem workerItem = this.getWorkerSnippet(); workerItem.setSortText(Priority.PRIORITY160.toString()); completionItems.add(workerItem); } } else if (previousNode instanceof BLangWorker) { completionItems.add(this.getWorkerSnippet()); } this.setPriorities(completionItems); }
From source file:org.ballerinalang.langserver.implementation.GotoImplementationCustomErrorStrategy.java
License:Open Source License
@Override public void reportMatch(Parser recognizer) { super.reportMatch(recognizer); if (recognizer.getSourceName().equals(relativeSourceFilePath) && !terminateCheck) { Token currentToken = recognizer.getCurrentToken(); // -1 added since the ANTLR line position is not zero based int tokenLine = currentToken.getLine() - 1; int tokenStartCol = currentToken.getCharPositionInLine(); int tokenStopCol = tokenStartCol + currentToken.getText().length(); if (this.line == tokenLine && this.col >= tokenStartCol && this.col <= tokenStopCol) { this.lsContext.put(GotoImplementationKeys.SYMBOL_TOKEN_KEY, currentToken.getText()); this.terminateCheck = true; }/*from w w w.j a v a 2s . co m*/ } }
From source file:org.ballerinalang.langserver.implementation.GotoImplementationCustomErrorStratergy.java
License:Open Source License
@Override public void reportMatch(Parser recognizer) { super.reportMatch(recognizer); if (recognizer.getContext().start.getTokenSource().getSourceName().equals( lsContext.get(DocumentServiceKeys.RELATIVE_FILE_PATH_KEY).replace("\\", "/")) && !terminateCheck) { Token currentToken = recognizer.getCurrentToken(); // -1 added since the ANTLR line position is not zero based int tokenLine = currentToken.getLine() - 1; int tokenStartCol = currentToken.getCharPositionInLine(); int tokenStopCol = tokenStartCol + currentToken.getText().length(); if (this.line == tokenLine && this.col >= tokenStartCol && this.col <= tokenStopCol) { this.lsContext.put(GotoImplementationKeys.SYMBOL_TOKEN_KEY, currentToken.getText()); this.terminateCheck = true; }//from ww w.ja v a 2s .com } }