use of org.eclipse.jdt.core.dom.StringLiteral in project sts4 by spring-projects.
the class ValueCompletionProcessor method provideCompletions.
@Override
public Collection<ICompletionProposal> provideCompletions(ASTNode node, Annotation annotation, ITypeBinding type, int offset, IDocument doc) {
List<ICompletionProposal> result = new ArrayList<>();
try {
FuzzyMap<PropertyInfo> index = indexProvider.getIndex(doc);
// case: @Value(<*>)
if (node == annotation && doc.get(offset - 1, 2).endsWith("()")) {
List<Match<PropertyInfo>> matches = findMatches("", index);
for (Match<PropertyInfo> match : matches) {
DocumentEdits edits = new DocumentEdits(doc);
edits.replace(offset, offset, "\"${" + match.data.getId() + "}\"");
ValuePropertyKeyProposal proposal = new ValuePropertyKeyProposal(edits, match.data.getId(), match.data.getName(), null);
result.add(proposal);
}
} else // case: @Value(prefix<*>)
if (node instanceof SimpleName && node.getParent() instanceof Annotation) {
computeProposalsForSimpleName(node, result, offset, doc, index);
} else // case: @Value(value=<*>)
if (node instanceof SimpleName && node.getParent() instanceof MemberValuePair && "value".equals(((MemberValuePair) node.getParent()).getName().toString())) {
computeProposalsForSimpleName(node, result, offset, doc, index);
} else // case: @Value("prefix<*>")
if (node instanceof StringLiteral && node.getParent() instanceof Annotation) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
computeProposalsForStringLiteral(node, result, offset, doc, index);
}
} else // case: @Value(value="prefix<*>")
if (node instanceof StringLiteral && node.getParent() instanceof MemberValuePair && "value".equals(((MemberValuePair) node.getParent()).getName().toString())) {
if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
computeProposalsForStringLiteral(node, result, offset, doc, index);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
use of org.eclipse.jdt.core.dom.StringLiteral in project sts4 by spring-projects.
the class WebfluxRouterSymbolProvider method extractPath.
private WebfluxRouteElement[] extractPath(MethodInvocation routerInvocation, TextDocument doc) {
WebfluxPathFinder pathFinder = new WebfluxPathFinder(routerInvocation, doc);
List<?> arguments = routerInvocation.arguments();
for (Object argument : arguments) {
if (argument != null && argument instanceof ASTNode) {
((ASTNode) argument).accept(pathFinder);
}
}
List<WebfluxRouteElement> path = pathFinder.getPath();
extractNestedValue(routerInvocation, path, (methodInvocation) -> {
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
String methodName = methodBinding.getName();
try {
if (WebfluxUtils.REQUEST_PREDICATE_PATH_METHOD.equals(methodName)) {
StringLiteral stringLiteral = WebfluxUtils.extractStringLiteralArgument(methodInvocation);
if (stringLiteral != null) {
Range range = doc.toRange(stringLiteral.getStartPosition(), stringLiteral.getLength());
return new WebfluxRouteElement(stringLiteral.getLiteralValue(), range);
}
}
} catch (BadLocationException e) {
// ignore
}
return null;
});
return (WebfluxRouteElement[]) path.toArray(new WebfluxRouteElement[path.size()]);
}
use of org.eclipse.jdt.core.dom.StringLiteral in project fabric8 by jboss-fuse.
the class AddSwaggerAnnotationMojo method addSwaggerApiAnnotation.
private void addSwaggerApiAnnotation(CompilationUnit unit, AnnotationVisitor visitor, File file, Document document) throws JavaModelException, IllegalArgumentException, MalformedTreeException, BadLocationException, IOException {
AST ast = unit.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
ListRewrite listRewrite = rewriter.getListRewrite(unit, CompilationUnit.TYPES_PROPERTY);
NormalAnnotation normalAnnotation = rewriter.getAST().newNormalAnnotation();
Name name = ast.newName("com.wordnik.swagger.annotations.Api");
normalAnnotation.setTypeName(name);
MemberValuePair memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("value"));
StringLiteral stringLiteral = ast.newStringLiteral();
String rootPath = visitor.getRootPath();
rootPath = rootPath.substring(1, rootPath.length() - 1);
if (rootPath.endsWith("/")) {
rootPath = rootPath.substring(0, rootPath.length() - 1);
}
stringLiteral.setLiteralValue(rootPath);
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("description"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue("Operations about " + visitor.getRestServiceClass());
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
listRewrite.insertAt(normalAnnotation, 0, null);
for (MethodDeclaration method : visitor.getRestMethod()) {
listRewrite = rewriter.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY);
normalAnnotation = rewriter.getAST().newNormalAnnotation();
name = ast.newName("com.wordnik.swagger.annotations.ApiOperation");
normalAnnotation.setTypeName(name);
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("value"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(method.getName().toString());
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
Javadoc doc = method.getJavadoc();
String comment = null;
if (doc != null) {
comment = method.getJavadoc().toString();
}
if (comment != null && comment.length() > 0) {
// add notes from method java doc
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("notes"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(comment);
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
}
listRewrite.insertAt(normalAnnotation, 0, null);
listRewrite = rewriter.getListRewrite((ASTNode) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0), SingleVariableDeclaration.MODIFIERS2_PROPERTY);
normalAnnotation = rewriter.getAST().newNormalAnnotation();
name = ast.newName("com.wordnik.swagger.annotations.ApiParam");
normalAnnotation.setTypeName(name);
((VariableDeclaration) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0)).getName();
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("value"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(((VariableDeclaration) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0)).getName().toString());
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
listRewrite.insertAt(normalAnnotation, 0, null);
}
TextEdit edits = rewriter.rewriteAST(document, null);
edits.apply(document);
FileUtils.writeStringToFile(file, document.get());
}
use of org.eclipse.jdt.core.dom.StringLiteral in project jbosstools-hibernate by jbosstools.
the class HQLQuickAssistProcessor method getAssists.
public IJavaCompletionProposal[] getAssists(final IInvocationContext context, IProblemLocation[] locations) throws CoreException {
IJavaCompletionProposal[] result = new IJavaCompletionProposal[0];
if (!hasAssists(context))
return result;
ASTNode coveringNode = context.getCoveringNode();
if (!(coveringNode instanceof StringLiteral)) {
return result;
}
final StringLiteral stringLiteral = (StringLiteral) coveringNode;
String contents = stringLiteral.getLiteralValue();
result = new IJavaCompletionProposal[1];
result[0] = new ExternalActionQuickAssistProposal(contents, EclipseImages.getImage(ImageConstants.HQL_EDITOR), JdtUiMessages.HQLQuickAssistProcessor_copy_to_hql_editor, context) {
public void apply(IDocument document) {
IEditorPart editorPart = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
ITextEditor[] textEditors = OpenMappingUtils.getTextEditors(editorPart);
if (textEditors.length == 0)
return;
Point position = new Point(stringLiteral.getStartPosition() + 1, stringLiteral.getLength() - 2);
new SaveQueryEditorListener(textEditors[0], getName(), getContents(), position, SaveQueryEditorListener.HQLEditor);
}
};
return result;
}
use of org.eclipse.jdt.core.dom.StringLiteral in project eclipse.jdt.ls by eclipse.
the class CompletionHandler method isCompletionForConstructor.
/**
* Check whether the completion is triggered for constructors: "new |"
* @param params completion parameters
* @param unit completion unit
* @param monitor progress monitor
* @throws JavaModelException
*/
private boolean isCompletionForConstructor(CompletionParams params, ICompilationUnit unit, IProgressMonitor monitor) throws JavaModelException {
Position pos = params.getPosition();
int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), pos.getLine(), pos.getCharacter());
if (offset < 4) {
return false;
}
String content = unit.getSource();
if (content == null) {
return false;
}
String triggerWord = content.substring(offset - 4, offset);
if (!"new ".equals(triggerWord)) {
return false;
}
CompilationUnit root = SharedASTProviderCore.getAST(unit, SharedASTProviderCore.WAIT_ACTIVE_ONLY, monitor);
if (root == null || monitor.isCanceled()) {
return false;
}
ASTNode node = NodeFinder.perform(root, offset - 4, 0);
if (node instanceof StringLiteral || node instanceof SimpleName) {
return false;
}
return true;
}
Aggregations