Java Code Examples for org.eclipse.jdt.core.compiler.IScanner#setSource()

The following examples show how to use org.eclipse.jdt.core.compiler.IScanner#setSource() . 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: Util.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static boolean isJustWhitespaceOrComment(int start, int end, IBuffer buffer) {
	if (start == end) {
		return true;
	}
	Assert.isTrue(start <= end);
	String trimmedText = buffer.getText(start, end - start).trim();
	if (0 == trimmedText.length()) {
		return true;
	} else {
		IScanner scanner = ToolFactory.createScanner(false, false, false, null);
		scanner.setSource(trimmedText.toCharArray());
		try {
			return scanner.getNextToken() == ITerminalSymbols.TokenNameEOF;
		} catch (InvalidInputException e) {
			return false;
		}
	}
}
 
Example 2
Source File: Util.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isJustWhitespaceOrComment(int start, int end, IBuffer buffer) {
	if (start == end)
		return true;
	Assert.isTrue(start <= end);
	String trimmedText= buffer.getText(start, end - start).trim();
	if (0 == trimmedText.length()) {
		return true;
	} else {
		IScanner scanner= ToolFactory.createScanner(false, false, false, null);
		scanner.setSource(trimmedText.toCharArray());
		try {
			return scanner.getNextToken() == ITerminalSymbols.TokenNameEOF;
		} catch (InvalidInputException e) {
			return false;
		}
	}
}
 
Example 3
Source File: CommentAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Removes comments and whitespace
 * @param reference the type reference
 * @return the reference only consisting of dots and java identifier characters
 */
public static String normalizeReference(String reference) {
	IScanner scanner= ToolFactory.createScanner(false, false, false, false);
	scanner.setSource(reference.toCharArray());
	StringBuffer sb= new StringBuffer();
	try {
		int tokenType= scanner.getNextToken();
		while (tokenType != ITerminalSymbols.TokenNameEOF) {
			sb.append(scanner.getRawTokenSource());
			tokenType= scanner.getNextToken();
		}
	} catch (InvalidInputException e) {
		Assert.isTrue(false, reference);
	}
	reference= sb.toString();
	return reference;
}
 
Example 4
Source File: MarkerUtil.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @param source
 *            must be not null
 * @param range
 *            can be null
 * @return may return null, otherwise an initialized scanner which may
 *         answer which source offset index belongs to which source line
 * @throws JavaModelException
 */
private static IScanner initScanner(IType source, ISourceRange range) throws JavaModelException {
    if (range == null) {
        return null;
    }
    char[] charContent = getContent(source);
    if (charContent == null) {
        return null;
    }
    IScanner scanner = ToolFactory.createScanner(false, false, false, true);
    scanner.setSource(charContent);
    int offset = range.getOffset();
    try {
        while (scanner.getNextToken() != ITerminalSymbols.TokenNameEOF) {
            // do nothing, just wait for the end of stream
            if (offset <= scanner.getCurrentTokenEndPosition()) {
                break;
            }
        }
    } catch (InvalidInputException e) {
        FindbugsPlugin.getDefault().logException(e, "Could not init scanner for type: " + source);
    }
    return scanner;
}
 
Example 5
Source File: GoToNextPreviousMemberAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static int firstOpeningBraceOffset(IInitializer iInitializer) throws JavaModelException {
	try {
		IScanner scanner= ToolFactory.createScanner(false, false, false, false);
		scanner.setSource(iInitializer.getSource().toCharArray());
		int token= scanner.getNextToken();
		while (token != ITerminalSymbols.TokenNameEOF && token != ITerminalSymbols.TokenNameLBRACE)
			token= scanner.getNextToken();
		if (token == ITerminalSymbols.TokenNameLBRACE)
			return iInitializer.getSourceRange().getOffset() + scanner.getCurrentTokenStartPosition() + scanner.getRawTokenSource().length;
		return iInitializer.getSourceRange().getOffset();
	} catch (InvalidInputException e) {
		return iInitializer.getSourceRange().getOffset();
	}
}
 
Example 6
Source File: NewTypeWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isValidComment(String template) {
	IScanner scanner= ToolFactory.createScanner(true, false, false, false);
	scanner.setSource(template.toCharArray());
	try {
		int next= scanner.getNextToken();
		while (TokenScanner.isComment(next)) {
			next= scanner.getNextToken();
		}
		return next == ITerminalSymbols.TokenNameEOF;
	} catch (InvalidInputException e) {
	}
	return false;
}
 
Example 7
Source File: NodeFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * A visitor that maps a selection to a given ASTNode. The result node is
 * determined as follows:
 * <ul>
 *   <li>first the visitor tries to find a node that is covered by <code>start</code> and
 *       <code>length</code> where either <code>start</code> and <code>length</code> exactly
 *       matches the node or where the text covered before and after the node only consists
 *       of white spaces or comments.</li>
 * 	 <li>if no such node exists than the node that encloses the range defined by
 *       start and end is returned.</li>
 *   <li>if the length is zero than also nodes are considered where the node's
 *       start or end position matches <code>start</code>.</li>
 *   <li>otherwise <code>null</code> is returned.</li>
 * </ul>
 *
 * @param root the root node from which the search starts
 * @param start the start offset
 * @param length the length
 * @param source the source of the compilation unit
 *
 * @return the result node
 * @throws JavaModelException if an error occurs in the Java model
 *
 * @since		3.0
 */
public static ASTNode perform(ASTNode root, int start, int length, ITypeRoot source) throws JavaModelException {
	NodeFinder finder= new NodeFinder(start, length);
	root.accept(finder);
	ASTNode result= finder.getCoveredNode();
	if (result == null)
		return null;
	Selection selection= Selection.createFromStartLength(start, length);
	if (selection.covers(result)) {
		IBuffer buffer= source.getBuffer();
		if (buffer != null) {
			IScanner scanner= ToolFactory.createScanner(false, false, false, false);
			scanner.setSource(buffer.getText(start, length).toCharArray());
			try {
				int token= scanner.getNextToken();
				if (token != ITerminalSymbols.TokenNameEOF) {
					int tStart= scanner.getCurrentTokenStartPosition();
					if (tStart == result.getStartPosition() - start) {
						scanner.resetTo(tStart + result.getLength(), length - 1);
						token= scanner.getNextToken();
						if (token == ITerminalSymbols.TokenNameEOF)
							return result;
					}
				}
			} catch (InvalidInputException e) {
			}
		}
	}
	return finder.getCoveringNode();
}
 
Example 8
Source File: ParameterObjectFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isValidComment(String template) {
	IScanner scanner= ToolFactory.createScanner(true, false, false, false);
	scanner.setSource(template.toCharArray());
	try {
		int next= scanner.getNextToken();
		while (TokenScanner.isComment(next)) {
			next= scanner.getNextToken();
		}
		return next == ITerminalSymbols.TokenNameEOF;
	} catch (InvalidInputException e) {
	}
	return false;
}
 
Example 9
Source File: TypeCreator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isValidComment(String template) {
  IScanner scanner = ToolFactory.createScanner(true, false, false, false);
  scanner.setSource(template.toCharArray());
  try {
    int next = scanner.getNextToken();
    while (TokenScanner.isComment(next)) {
      next = scanner.getNextToken();
    }
    return next == ITerminalSymbols.TokenNameEOF;
  } catch (InvalidInputException e) {
    // If there are lexical errors, the comment is invalid
  }
  return false;
}
 
Example 10
Source File: SJavaTokenComparator.java    From coming with MIT License 5 votes vote down vote up
/**
 * Creates a token comparator for the given string.
 *
 * @param text the text to be tokenized
 * @param textTokenComparatorFactory a factory to create text token comparators
 */
public SJavaTokenComparator(String text) {


  fText= text;

  int length= fText.length();
  fStarts= new int[length];
  fLengths= new int[length];
  fTokens= new String[length];
  fCount= 0;

  IScanner scanner= ToolFactory.createScanner(true, false, false, false); // returns comments & whitespace
  scanner.setSource(fText.toCharArray());
  int endPos= 0;
  try {
    int tokenType;
    while ((tokenType= scanner.getNextToken()) != ITerminalSymbols.TokenNameEOF) {
      int start= scanner.getCurrentTokenStartPosition();
      int end= scanner.getCurrentTokenEndPosition()+1;
      // Comments are treated as a single token (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=78063)
      if (!TokenScanner.isComment(tokenType)) {
        recordTokenRange(start, end - start);
      }
      endPos= end;
    }
  } catch (InvalidInputException ex) {
    // We couldn't parse part of the input. Fall through and make the rest a single token
  }
  // Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=13907
  if (endPos < length) {
    recordTokenRange(endPos, length - endPos);
  }
}
 
Example 11
Source File: MethodOccurenceCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException {
	if (match instanceof MethodReferenceMatch
			&& ((MethodReferenceMatch) match).isSuperInvocation()
			&& match.getAccuracy() == SearchMatch.A_INACCURATE) {
		return; // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=156491
	}

	if (match.isImplicit()) { // see bug 94062
		collectMatch(match);
		return;
	}

	int start= match.getOffset();
	int length= match.getLength();
	String matchText= unit.getBuffer().getText(start, length);

	//direct match:
	if (fName.equals(matchText)) {
		collectMatch(match);
		return;
	}

	// lambda expression
	if (match instanceof MethodDeclarationMatch
			&& match.getElement() instanceof IMethod
			&& ((IMethod) match.getElement()).isLambdaMethod()) {
		// don't touch the lambda
		return;
	}

	//Not a standard reference -- use scanner to find last identifier token before left parenthesis:
	IScanner scanner= getScanner(unit);
	scanner.setSource(matchText.toCharArray());
	int simpleNameStart= -1;
	int simpleNameEnd= -1;
	try {
		int token = scanner.getNextToken();
		while (token != ITerminalSymbols.TokenNameEOF && token != ITerminalSymbols.TokenNameLPAREN) { // reference in code includes arguments in parentheses
			if (token == ITerminalSymbols.TokenNameIdentifier) {
				simpleNameStart= scanner.getCurrentTokenStartPosition();
				simpleNameEnd= scanner.getCurrentTokenEndPosition();
			}
			token = scanner.getNextToken();
		}
	} catch (InvalidInputException e){
		//ignore
	}
	if (simpleNameStart != -1) {
		match.setOffset(start + simpleNameStart);
		match.setLength(simpleNameEnd + 1 - simpleNameStart);
	}
	collectMatch(match);
}
 
Example 12
Source File: TypeOccurrenceCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public SearchMatch acceptSearchMatch2(ICompilationUnit unit, SearchMatch match) throws CoreException {
	int start= match.getOffset();
	int length= match.getLength();

	//unqualified:
	String matchText= unit.getBuffer().getText(start, length);
	if (fOldName.equals(matchText)) {
		return match;
	}

	//(partially) qualified:
	if (fOldQualifiedName.endsWith(matchText)) {
		//e.g. rename B and p.A.B ends with match A.B
		int simpleNameLenght= fOldName.length();
		match.setOffset(start + length - simpleNameLenght);
		match.setLength(simpleNameLenght);
		return match;
	}

	//Not a standard reference -- use scanner to find last identifier token:
	IScanner scanner= getScanner(unit);
	scanner.setSource(matchText.toCharArray());
	int simpleNameStart= -1;
	int simpleNameEnd= -1;
	try {
		int token = scanner.getNextToken();
		while (token != ITerminalSymbols.TokenNameEOF) {
			if (token == ITerminalSymbols.TokenNameIdentifier) {
				simpleNameStart= scanner.getCurrentTokenStartPosition();
				simpleNameEnd= scanner.getCurrentTokenEndPosition();
			}
			token = scanner.getNextToken();
		}
	} catch (InvalidInputException e){
		//ignore
	}
	if (simpleNameStart != -1) {
		match.setOffset(start + simpleNameStart);
		match.setLength(simpleNameEnd + 1 - simpleNameStart);
	}
	return match;
}
 
Example 13
Source File: FoldingRangeHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void computeFoldingRanges(List<FoldingRange> foldingRanges, ITypeRoot unit, IProgressMonitor monitor) {
	try {
		ISourceRange range = unit.getSourceRange();
		if (!SourceRange.isAvailable(range)) {
			return;
		}

		String contents = unit.getSource();
		if (StringUtils.isBlank(contents)) {
			return;
		}

		final int shift = range.getOffset();
		IScanner scanner = getScanner();
		scanner.setSource(contents.toCharArray());
		scanner.resetTo(shift, shift + range.getLength());

		int start = shift;
		int token = 0;
		Stack<Integer> regionStarts = new Stack<>();
		while (token != ITerminalSymbols.TokenNameEOF) {
			start = scanner.getCurrentTokenStartPosition();
			switch (token) {
				case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
				case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
					int end = scanner.getCurrentTokenEndPosition();
					FoldingRange commentFoldingRange = new FoldingRange(scanner.getLineNumber(start) - 1, scanner.getLineNumber(end) - 1);
					commentFoldingRange.setKind(FoldingRangeKind.Comment);
					foldingRanges.add(commentFoldingRange);
					break;
				case ITerminalSymbols.TokenNameCOMMENT_LINE:
					String currentSource = String.valueOf(scanner.getCurrentTokenSource());
					if (REGION_START_PATTERN.matcher(currentSource).lookingAt()) {
						regionStarts.push(start);
					} else if (REGION_END_PATTERN.matcher(currentSource).lookingAt()) {
						if (regionStarts.size() > 0) {
							FoldingRange regionFolding = new FoldingRange(scanner.getLineNumber(regionStarts.pop()) - 1, scanner.getLineNumber(start) - 1);
							regionFolding.setKind(FoldingRangeKind.Region);
							foldingRanges.add(regionFolding);
						}
					}
					break;
				default:
					break;
			}
			token = getNextToken(scanner);
		}
		computeTypeRootRanges(foldingRanges, unit, scanner);
	} catch (CoreException e) {
		JavaLanguageServerPlugin.logException("Problem with folding range for " + unit.getPath().toPortableString(), e);
		monitor.setCanceled(true);
	}
}
 
Example 14
Source File: SelectionAwareSourceRangeComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void initializeRanges() throws CoreException {
	fRanges= new HashMap<ASTNode, SourceRange>();
	if (fSelectedNodes.length == 0)
		return;

	fRanges.put(fSelectedNodes[0], super.computeSourceRange(fSelectedNodes[0]));
	int last= fSelectedNodes.length - 1;
	fRanges.put(fSelectedNodes[last], super.computeSourceRange(fSelectedNodes[last]));

	IScanner scanner= ToolFactory.createScanner(true, false, false, false);
	char[] source= fDocumentPortionToScan.toCharArray();
	scanner.setSource(source);
	fDocumentPortionToScan= null; // initializeRanges() is only called once

	TokenScanner tokenizer= new TokenScanner(scanner);
	int pos= tokenizer.getNextStartOffset(0, false);

	ASTNode currentNode= fSelectedNodes[0];
	int newStart= Math.min(fSelectionStart + pos, currentNode.getStartPosition());
	SourceRange range= fRanges.get(currentNode);
	fRanges.put(currentNode, new SourceRange(newStart, range.getLength() + range.getStartPosition() - newStart));

	currentNode= fSelectedNodes[last];
	int scannerStart= currentNode.getStartPosition() + currentNode.getLength() - fSelectionStart;
	tokenizer.setOffset(scannerStart);
	pos= scannerStart;
	int token= -1;
	try {
		while (true) {
			token= tokenizer.readNext(false);
			pos= tokenizer.getCurrentEndOffset();
		}
	} catch (CoreException e) {
	}
	if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
		int index= pos - 1;
		while (index >= 0 && IndentManipulation.isLineDelimiterChar(source[index])) {
			pos--;
			index--;
		}
	}

	int newEnd= Math.max(fSelectionStart + pos, currentNode.getStartPosition() + currentNode.getLength());
	range= fRanges.get(currentNode);
	fRanges.put(currentNode, new SourceRange(range.getStartPosition(), newEnd - range.getStartPosition()));
}
 
Example 15
Source File: SelectionAwareSourceRangeComputer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void initializeRanges() throws CoreException {
	fRanges = new HashMap<>();
	if (fSelectedNodes.length == 0) {
		return;
	}

	fRanges.put(fSelectedNodes[0], super.computeSourceRange(fSelectedNodes[0]));
	int last = fSelectedNodes.length - 1;
	fRanges.put(fSelectedNodes[last], super.computeSourceRange(fSelectedNodes[last]));

	IJavaProject javaProject = ((CompilationUnit) fSelectedNodes[0].getRoot()).getTypeRoot().getJavaProject();
	String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
	String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
	IScanner scanner = ToolFactory.createScanner(true, false, false, sourceLevel, complianceLevel);
	char[] source = fDocumentPortionToScan.toCharArray();
	scanner.setSource(source);
	fDocumentPortionToScan = null; // initializeRanges() is only called once

	TokenScanner tokenizer = new TokenScanner(scanner);
	int pos = tokenizer.getNextStartOffset(0, false);

	ASTNode currentNode = fSelectedNodes[0];
	int newStart = Math.min(fSelectionStart + pos, currentNode.getStartPosition());
	SourceRange range = fRanges.get(currentNode);
	fRanges.put(currentNode, new SourceRange(newStart, range.getLength() + range.getStartPosition() - newStart));

	currentNode = fSelectedNodes[last];
	int scannerStart = currentNode.getStartPosition() + currentNode.getLength() - fSelectionStart;
	tokenizer.setOffset(scannerStart);
	pos = scannerStart;
	int token = -1;
	try {
		while (true) {
			token = tokenizer.readNext(false);
			pos = tokenizer.getCurrentEndOffset();
		}
	} catch (CoreException e) {
	}
	if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
		int index = pos - 1;
		while (index >= 0 && IndentManipulation.isLineDelimiterChar(source[index])) {
			pos--;
			index--;
		}
	}

	int newEnd = Math.max(fSelectionStart + pos, currentNode.getStartPosition() + currentNode.getLength());
	range = fRanges.get(currentNode);
	fRanges.put(currentNode, new SourceRange(range.getStartPosition(), newEnd - range.getStartPosition()));

	// The extended source range of the last child node can end after the selection.
	// We have to ensure that the source range of such child nodes is also capped by the selection range.
	// Example: (/*]*/TRANSFORMER::transform/*[*/)
	// Selection is between /*]*/ and /*[*/, but the extended range of the "transform" node actually includes /*[*/ as well.
	while (true) {
		List<ASTNode> children = ASTNodes.getChildren(currentNode);
		if (!children.isEmpty()) {
			ASTNode lastChild = children.get(children.size() - 1);
			SourceRange extRange = super.computeSourceRange(lastChild);
			if (extRange.getStartPosition() + extRange.getLength() > newEnd) {
				fRanges.put(lastChild, new SourceRange(extRange.getStartPosition(), newEnd - extRange.getStartPosition()));
				currentNode = lastChild;
				continue;
			}
		}
		break;
	}
}
 
Example 16
Source File: NLSSearchResultRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Finds the key defined by the given match. The assumption is that the key is the only argument
 * and it is a string literal i.e. quoted ("...") or a string constant i.e. 'static final
 * String' defined in the same class.
 * 
 * @param keyPositionResult reference parameter: will be filled with the position of the found
 *            key
 * @param enclosingElement enclosing java element
 * @return a string denoting the key, {@link #NO_KEY} if no key can be found and
 *         <code>null</code> otherwise
 * @throws CoreException if a problem occurs while accessing the <code>enclosingElement</code>
 */
private String findKey(Position keyPositionResult, IJavaElement enclosingElement) throws CoreException {
	ICompilationUnit unit= (ICompilationUnit)enclosingElement.getAncestor(IJavaElement.COMPILATION_UNIT);
	if (unit == null)
		return null;

	String source= unit.getSource();
	if (source == null)
		return null;

	IJavaProject javaProject= unit.getJavaProject();
	IScanner scanner= null;
	if (javaProject != null) {
		String complianceLevel= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
		String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
		scanner= ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel);
	} else {
		scanner= ToolFactory.createScanner(false, false, false, false);
	}
	scanner.setSource(source.toCharArray());
	scanner.resetTo(keyPositionResult.getOffset() + keyPositionResult.getLength(), source.length());

	try {
		if (scanner.getNextToken() != ITerminalSymbols.TokenNameDOT)
			return null;

		if (scanner.getNextToken() != ITerminalSymbols.TokenNameIdentifier)
			return null;

		String src= new String(scanner.getCurrentTokenSource());
		int tokenStart= scanner.getCurrentTokenStartPosition();
		int tokenEnd= scanner.getCurrentTokenEndPosition();

		if (scanner.getNextToken() == ITerminalSymbols.TokenNameLPAREN) {
			// Old school
			// next must be key string. Ignore methods which do not take a single String parameter (Bug 295040).
			int nextToken= scanner.getNextToken();
			if (nextToken != ITerminalSymbols.TokenNameStringLiteral && nextToken != ITerminalSymbols.TokenNameIdentifier)
				return null;

			tokenStart= scanner.getCurrentTokenStartPosition();
			tokenEnd= scanner.getCurrentTokenEndPosition();
			int token;
			while ((token= scanner.getNextToken()) == ITerminalSymbols.TokenNameDOT) {
				if ((nextToken= scanner.getNextToken()) != ITerminalSymbols.TokenNameIdentifier) {
						return null;
				}
				tokenStart= scanner.getCurrentTokenStartPosition();
				tokenEnd= scanner.getCurrentTokenEndPosition();
			}
			if (token != ITerminalSymbols.TokenNameRPAREN)
				return null;
			
			if (nextToken == ITerminalSymbols.TokenNameStringLiteral) {
				keyPositionResult.setOffset(tokenStart + 1);
				keyPositionResult.setLength(tokenEnd - tokenStart - 1);
				return source.substring(tokenStart + 1, tokenEnd);
			} else if (nextToken == ITerminalSymbols.TokenNameIdentifier) {
				keyPositionResult.setOffset(tokenStart);
				keyPositionResult.setLength(tokenEnd - tokenStart + 1);
				IType parentClass= (IType)enclosingElement.getAncestor(IJavaElement.TYPE);
				IField[] fields= parentClass.getFields();
				String identifier= source.substring(tokenStart, tokenEnd + 1);
				for (int i= 0; i < fields.length; i++) {
					if (fields[i].getElementName().equals(identifier)) {
						if (!Signature.getSignatureSimpleName(fields[i].getTypeSignature()).equals("String")) //$NON-NLS-1$
							return null;
						Object obj= fields[i].getConstant();
						return obj instanceof String ? ((String)obj).substring(1, ((String)obj).length() - 1) : NO_KEY;
					}
				}
			}
			return NO_KEY;
		} else {
			IJavaElement[] keys= unit.codeSelect(tokenStart, tokenEnd - tokenStart + 1);

			// an interface can't be a key
			if (keys.length == 1 && keys[0].getElementType() == IJavaElement.TYPE && ((IType) keys[0]).isInterface())
				return null;

			keyPositionResult.setOffset(tokenStart);
			keyPositionResult.setLength(tokenEnd - tokenStart + 1);
			return src;
		}
	} catch (InvalidInputException e) {
		throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
	}
}
 
Example 17
Source File: TypeOccurrenceCollector.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public SearchMatch acceptSearchMatch2(ICompilationUnit unit, SearchMatch match) throws CoreException {
	int start= match.getOffset();
	int length= match.getLength();

	//unqualified:
	String matchText= unit.getBuffer().getText(start, length);
	if (fOldName.equals(matchText)) {
		return match;
	}

	//(partially) qualified:
	if (fOldQualifiedName.endsWith(matchText)) {
		//e.g. rename B and p.A.B ends with match A.B
		int simpleNameLenght= fOldName.length();
		match.setOffset(start + length - simpleNameLenght);
		match.setLength(simpleNameLenght);
		return match;
	}

	//Not a standard reference -- use scanner to find last identifier token:
	IScanner scanner= getScanner(unit);
	scanner.setSource(matchText.toCharArray());
	int simpleNameStart= -1;
	int simpleNameEnd= -1;
	try {
		int token = scanner.getNextToken();
		while (token != ITerminalSymbols.TokenNameEOF) {
			if (token == ITerminalSymbols.TokenNameIdentifier) { // type reference can occur in module-info.java and collide with a restricted keyword.
				simpleNameStart= scanner.getCurrentTokenStartPosition();
				simpleNameEnd= scanner.getCurrentTokenEndPosition();
			}
			token = scanner.getNextToken();
		}
	} catch (InvalidInputException e){
		//ignore
	}
	if (simpleNameStart != -1) {
		match.setOffset(start + simpleNameStart);
		match.setLength(simpleNameEnd + 1 - simpleNameStart);
	}
	return match;
}
 
Example 18
Source File: Member.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public ISourceRange getJavadocRange() throws JavaModelException {
	ISourceRange range= getSourceRange();
	if (range == null) return null;
	IBuffer buf= null;
	if (isBinary()) {
		buf = getClassFile().getBuffer();
	} else {
		ICompilationUnit compilationUnit = getCompilationUnit();
		if (!compilationUnit.isConsistent()) {
			return null;
		}
		buf = compilationUnit.getBuffer();
	}
	final int start= range.getOffset();
	final int length= range.getLength();
	if (length > 0 && buf.getChar(start) == '/') {
		IScanner scanner= ToolFactory.createScanner(true, false, false, false);
		try {
			scanner.setSource(buf.getText(start, length).toCharArray());
			int docOffset= -1;
			int docEnd= -1;

			int terminal= scanner.getNextToken();
			loop: while (true) {
				switch(terminal) {
					case ITerminalSymbols.TokenNameCOMMENT_JAVADOC :
						docOffset= scanner.getCurrentTokenStartPosition();
						docEnd= scanner.getCurrentTokenEndPosition() + 1;
						terminal= scanner.getNextToken();
						break;
					case ITerminalSymbols.TokenNameCOMMENT_LINE :
					case ITerminalSymbols.TokenNameCOMMENT_BLOCK :
						terminal= scanner.getNextToken();
						continue loop;
					default :
						break loop;
				}
			}
			if (docOffset != -1) {
				return new SourceRange(docOffset + start, docEnd - docOffset);
			}
		} catch (InvalidInputException ex) {
			// try if there is inherited Javadoc
		} catch (IndexOutOfBoundsException e) {
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=305001
		}
	}
	return null;
}
 
Example 19
Source File: MethodOccurenceCollector.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException {
	if (match instanceof MethodReferenceMatch
			&& ((MethodReferenceMatch) match).isSuperInvocation()
			&& match.getAccuracy() == SearchMatch.A_INACCURATE) {
		return; // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=156491
	}

	if (match.isImplicit()) { // see bug 94062
		collectMatch(match);
		return;
	}

	int start= match.getOffset();
	int length= match.getLength();
	String matchText= unit.getBuffer().getText(start, length);

	//direct match:
	if (fName.equals(matchText)) {
		collectMatch(match);
		return;
	}

	// lambda expression
	if (match instanceof MethodDeclarationMatch
			&& match.getElement() instanceof IMethod
			&& ((IMethod) match.getElement()).isLambdaMethod()) {
		// don't touch the lambda
		return;
	}

	//Not a standard reference -- use scanner to find last identifier token before left parenthesis:
	IScanner scanner= getScanner(unit);
	scanner.setSource(matchText.toCharArray());
	int simpleNameStart= -1;
	int simpleNameEnd= -1;
	try {
		int token = scanner.getNextToken();
		while (token != ITerminalSymbols.TokenNameEOF && token != ITerminalSymbols.TokenNameLPAREN) { // reference in code includes arguments in parentheses
			if (token == InternalTokenNameIdentifier) {
				simpleNameStart= scanner.getCurrentTokenStartPosition();
				simpleNameEnd= scanner.getCurrentTokenEndPosition();
			}
			token = scanner.getNextToken();
		}
	} catch (InvalidInputException e){
		//ignore
	}
	if (simpleNameStart != -1) {
		match.setOffset(start + simpleNameStart);
		match.setLength(simpleNameEnd + 1 - simpleNameStart);
	}
	collectMatch(match);
}
 
Example 20
Source File: JavaLexer.java    From SLP-Core with MIT License 4 votes vote down vote up
public List<List<String>> tokenizeLines(String text) {
	IScanner scanner = ToolFactory.createScanner(false, false, true, "1.8");
	scanner.setSource(text.toCharArray());
	List<List<String>> lineTokens = new ArrayList<>();
	List<String> tokens = new ArrayList<>();
	lineTokens.add(new ArrayList<>());
	int nextToken = 0;
	int line = 1;
	while (true) {
		try {
			nextToken = scanner.getNextToken();
			int ln = scanner.getLineNumber(scanner.getCurrentTokenStartPosition());
			if (ln > line) {
				for (int i = line + 1; i <= ln; i++) lineTokens.add(new ArrayList<>());
				line = ln;
			}
			if (nextToken == ITerminalSymbols.TokenNameEOF) break;
		} catch (InvalidInputException e) {
			continue;
		}
		String val = new String(scanner.getCurrentTokenSource());
		if (val.startsWith("\"") && val.endsWith("\"") && val.length() > 2) {
			if (val.length() >= 15) {
				val = "\"\"";
			}
			else {
				String body = val.substring(1, val.length() - 1);
				body = body.replaceAll("\\\\", "\\\\\\\\");
				body = body.replaceAll("\"", "\\\\\"");
				body = body.replaceAll("\n", "\\n");
				body = body.replaceAll("\r", "\\r");
				body = body.replaceAll("\t", "\\t");
				val = "\"" + body + "\"";
			}
		}
		else if (val.startsWith("\'") && val.endsWith("\'")) {
			val = val.replaceAll("\n", "\\n");
			val = val.replaceAll("\r", "\\r");
			val = val.replaceAll("\t", "\\t");
		}
		// For Java, we have to add heuristic check regarding breaking up >>
		else if (val.matches(">>+")) {
			boolean split = false;
			for (int i = tokens.size() - 1; i >= 0; i--) {
				String token = tokens.get(i);
				if (token.matches("[,\\.\\?\\[\\]]") || Character.isUpperCase(token.charAt(0))
						|| token.equals("extends") || token.equals("super")
						|| token.matches("(byte|short|int|long|float|double)")) {
					continue;
				}
				else if (token.matches("(<|>)+")) {
					split = true;
					break;
				}
				else {
					break;
				}
			}
			if (split) {
				for (int i = 0; i < val.length(); i++) {
					tokens.add(">");
					lineTokens.get(lineTokens.size() - 1).add(">");
				}
				continue;
			}
		}
		tokens.add(val);
		lineTokens.get(lineTokens.size() - 1).add(val);
	}
	return lineTokens;
}