org.eclipse.jface.text.codemining.ICodeMining Java Examples

The following examples show how to use org.eclipse.jface.text.codemining.ICodeMining. 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: JavaLaunchingCodeMiningProvider.java    From jdt-codemining with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
		IProgressMonitor monitor) {
	return CompletableFuture.supplyAsync(() -> {
		monitor.isCanceled();
		ITextEditor textEditor = super.getAdapter(ITextEditor.class);
		ITypeRoot unit = EditorUtility.getEditorInputJavaElement(textEditor, true);
		if (unit == null) {
			return Collections.emptyList();
		}
		try {
			IJavaElement[] elements = unit.getChildren();
			List<ICodeMining> minings = new ArrayList<>(elements.length);
			collectMinings(unit, textEditor, unit.getChildren(), minings, viewer, monitor);
			monitor.isCanceled();
			return minings;
		} catch (JavaModelException e) {
			// Should never occur
		}
		return Collections.emptyList();
	});
}
 
Example #2
Source File: AbstractXtextCodeMiningProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates code minings for a document
 * @param document The document
 * @param resource The resource for that document
 * @param indicator Cancelation indicator
 * @return Computed minings
 * @throws BadLocationException when line number doesn't exists
 */
protected List<ICodeMining> createCodeMinings(IDocument document, XtextResource resource,
		CancelIndicator cancelIndicator)
		throws BadLocationException {
	List<ICodeMining> codeMiningList = new ArrayList<>();
	IAcceptor<ICodeMining> acceptor = new IAcceptor<ICodeMining>() {
		@Override
		public void accept(ICodeMining codeMiningObject) {
			if (cancelIndicator.isCanceled()) {
				// do not accept mining and abort processing when operation was canceled
				throw new CancellationException();
			}
			codeMiningList.add(codeMiningObject);
		}
	};
	
	createCodeMinings(document, resource, cancelIndicator, acceptor);

	return codeMiningList;
}
 
Example #3
Source File: JUnitCodeMiningProvider.java    From jdt-codemining with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
		IProgressMonitor monitor) {
	this.viewer = viewer;
	return CompletableFuture.supplyAsync(() -> {
		monitor.isCanceled();
		ITextEditor textEditor = super.getAdapter(ITextEditor.class);
		ITypeRoot unit = EditorUtility.getEditorInputJavaElement(textEditor, true);
		if (unit == null) {
			return null;
		}
		try {
			IJavaElement[] elements = unit.getChildren();
			List<ICodeMining> minings = new ArrayList<>(elements.length);
			collectCodeMinings(unit, elements, minings, viewer, monitor);
			monitor.isCanceled();
			return minings;
		} catch (JavaModelException e) {
			// TODO: what should we done when there are some errors?
		}
		return null;
	});
}
 
Example #4
Source File: JavaASTCodeMiningProvider.java    From jdt-codemining with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
		IProgressMonitor monitor) {
	return CompletableFuture.supplyAsync(() -> {
		monitor.isCanceled();
		ITextEditor textEditor = super.getAdapter(ITextEditor.class);
		ITypeRoot unit = EditorUtility.getEditorInputJavaElement(textEditor, true);
		if (unit == null) {
			return null;
		}
		List<ICodeMining> minings = new ArrayList<>();
		collectCodeMinings(unit, textEditor, viewer, minings);
		monitor.isCanceled();
		return minings;
	});
}
 
Example #5
Source File: AbstractXtextCodeMiningProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
		IProgressMonitor monitor) {
	CompletableFuture<List<? extends ICodeMining>> future = CompletableFuture.supplyAsync(() -> {
		CancelableUnitOfWork<List<ICodeMining>, XtextResource> uow = new CancelableUnitOfWork<List<ICodeMining>, XtextResource>() {
			@Override
			public List<ICodeMining> exec(XtextResource resource, CancelIndicator uowCancelIndicator) throws Exception {
				CombinedCancelIndicator indicator = new CombinedCancelIndicator(monitor, uowCancelIndicator);
				return createCodeMinings(viewer.getDocument(), resource, indicator);
			}

		};
		return xtextDocumentUtil.getXtextDocument(viewer).tryReadOnly(uow, () -> Collections.emptyList());
	});
	return future;
}
 
Example #6
Source File: JavaElementCodeMiningProvider.java    From jdt-codemining with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
		IProgressMonitor monitor) {
	return CompletableFuture.supplyAsync(() -> {
		monitor.isCanceled();
		ITextEditor textEditor = super.getAdapter(ITextEditor.class);
		ITypeRoot unit = EditorUtility.getEditorInputJavaElement(textEditor, true);
		if (unit == null) {
			return Collections.emptyList();
		}
		fViewer = viewer;
		fUnit = unit;
		try {
			List<ICodeMining> minings = new ArrayList<>();
			collectMinings(unit, textEditor, unit.getChildren(), minings, viewer, monitor);
			monitor.isCanceled();
			return minings;
		} catch (JavaModelException e) {
			// TODO: what should we done when there are some errors?
		}
		return Collections.emptyList();
	});
}
 
Example #7
Source File: SARLCodeMiningProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
protected void createCodeMinings(IDocument document, XtextResource resource, CancelIndicator indicator,
		IAcceptor<? super ICodeMining> acceptor) throws BadLocationException {
	createImplicitActionReturnType(resource, acceptor);
	createImplicitFieldType(resource, acceptor);
	createImplicitVariableType(resource, acceptor);
}
 
Example #8
Source File: SARLCodeMiningProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer, IProgressMonitor monitor) {
	if (this.codeminingPreferences.isCodeminingEnabled()) {
		return super.provideCodeMinings(viewer, monitor);
	}
	return null;
}
 
Example #9
Source File: SARLCodeMiningProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Add an annotation when the variable's type is implicit and inferred by the SARL compiler.
 *
 * @param resource the resource to parse.
 * @param acceptor the code mining acceptor.
 */
private void createImplicitVariableType(XtextResource resource, IAcceptor<? super ICodeMining> acceptor) {
	createImplicitVarValType(resource, acceptor, XtendVariableDeclaration.class,
		it -> it.getType(),
		it -> {
			LightweightTypeReference type = getLightweightType(it.getRight());
			if (type.isAny()) {
				type = getTypeForVariableDeclaration(it.getRight());
			}
			return type.getSimpleName();
		},
		it -> it.getRight(),
		() -> this.grammar.getXVariableDeclarationAccess().getRightAssignment_3_1());
}
 
Example #10
Source File: ArithmeticsCodeMiningProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void createCodeMinings(IDocument document, XtextResource resource, CancelIndicator indicator,
	IAcceptor<? super ICodeMining> acceptor) throws BadLocationException {

	EList<EObject> contents = resource.getContents();
	if (contents.isEmpty()) {
		return;
	}

	// get all evaluations contained by the open document
	List<Evaluation> allEvaluations = EcoreUtil2.eAllOfType(contents.get(0), Evaluation.class);

	// get keyword for ';'
	Keyword semicolon = grammar.getEvaluationAccess().getSemicolonKeyword_1();

	for (Evaluation evaluation : allEvaluations) {
		ICompositeNode node = NodeModelUtils.findActualNodeFor(evaluation);
		for (Iterator<INode> it = node.getAsTreeIterable().iterator(); it.hasNext();) {
			INode child = it.next();
			if (semicolon.equals(child.getGrammarElement())) {
				int annotationOffset = child.getTotalOffset();
				String annotationText = getAnnotationText(evaluation);
				acceptor.accept(createNewLineContentCodeMining(annotationOffset, annotationText));
			}
		}
	}
}
 
Example #11
Source File: DomainmodelCodeMiningProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void createCodeMinings(IDocument document, XtextResource resource, CancelIndicator indicator,
		IAcceptor<? super ICodeMining> acceptor) throws BadLocationException {
	if (resource.getContents().isEmpty()) {
		return;
	}
	// get all operations to open document
	List<Operation> allOperations = EcoreUtil2.eAllOfType(resource.getContents().get(0), Operation.class);
	// get keyword for ')'
	Keyword rightParenthesisKeyword_4 = grammar.getOperationAccess().getRightParenthesisKeyword_4();
	for (Operation o : allOperations) {
		//inline annotations only for methods with no return type
		if (o.getType() != null) {
			continue;
		}
		// get return type name from operation
		JvmOperation inferredOp = (JvmOperation) jvmModelAssociations.getPrimaryJvmElement(o);
		if (inferredOp == null || inferredOp.getReturnType() == null) {
			continue; // broken model
		}
		String returnTypeName = inferredOp.getReturnType().getSimpleName();
		// find document offset for inline annotation
		ICompositeNode node = NodeModelUtils.findActualNodeFor(o);
		for (Iterator<INode> it = node.getAsTreeIterable().iterator(); it.hasNext();) {
			INode child = it.next();
			if (rightParenthesisKeyword_4.equals(child.getGrammarElement())) {
				// create line content code mining for inline annotation after grammarElement ')'
				String annotationText = " : " + returnTypeName;
				acceptor.accept(createNewLineContentCodeMining(child.getTotalOffset() + 1, annotationText));
			}
		}
	}
}
 
Example #12
Source File: SARLCodeMiningProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Add an annotation when the field's type is implicit and inferred by the SARL compiler.
 *
 * @param resource the resource to parse.
 * @param acceptor the code mining acceptor.
 */
private void createImplicitFieldType(XtextResource resource, IAcceptor<? super ICodeMining> acceptor) {
	createImplicitVarValType(resource, acceptor, XtendField.class,
		it -> it.getType(),
		it -> {
			final JvmField inferredField = (JvmField) this.jvmModelAssocitions.getPrimaryJvmElement(it);
			if (inferredField == null || inferredField.getType() == null || inferredField.getType().eIsProxy()) {
				return null;
			}
			return inferredField.getType().getSimpleName();
		},
		null,
		() -> this.grammar.getAOPMemberAccess().getInitialValueAssignment_2_3_3_1());
}
 
Example #13
Source File: JavaCodeMiningASTVisitor.java    From jdt-codemining with Eclipse Public License 1.0 5 votes vote down vote up
public JavaCodeMiningASTVisitor(CompilationUnit cu, ITextEditor textEditor, ITextViewer viewer,
		List<ICodeMining> minings, ICodeMiningProvider provider) {
	this.cu = cu;
	this.minings = minings;
	this.provider = provider;
	this.textEditor = textEditor;
	this.viewer = viewer;
}
 
Example #14
Source File: JavaASTCodeMiningProvider.java    From jdt-codemining with Eclipse Public License 1.0 5 votes vote down vote up
private void collectCodeMinings(ITypeRoot unit, ITextEditor textEditor, ITextViewer viewer,
		List<ICodeMining> minings) {
	CompilationUnit cu = SharedASTProvider.getAST(unit, SharedASTProvider.WAIT_YES, null);
	JavaCodeMiningASTVisitor visitor = new JavaCodeMiningASTVisitor(cu, textEditor, viewer, minings, this);
	visitor.setShowParameterName(showParameterName);
	visitor.setShowParameterType(showParameterType);
	visitor.setShowParameterOnlyForLiteral(showParameterOnlyForLiteral);
	visitor.setShowParameterByUsingFilters(showParameterByUsingFilters);
	visitor.setShowEndStatement(showEndStatement);
	visitor.setEndStatementMinLineNumber(endStatementMinLineNumber);
	visitor.setShowJava10VarType(showJava10VarType);
	cu.accept(visitor);
}
 
Example #15
Source File: JavaDebugElementCodeMiningProvider.java    From jdt-codemining with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected List provideCodeMinings(ITextViewer viewer, IJavaStackFrame frame, IProgressMonitor monitor) {
	List<ICodeMining> minings = new ArrayList<>();
	ITextEditor textEditor = super.getAdapter(ITextEditor.class);
	ITypeRoot unit = EditorUtility.getEditorInputJavaElement(textEditor, true);
	if (unit == null) {
		return minings;
	}
	CompilationUnit cu = SharedASTProvider.getAST(unit, SharedASTProvider.WAIT_YES, null);
	JavaDebugElementCodeMiningASTVisitor visitor = new JavaDebugElementCodeMiningASTVisitor(frame, cu, viewer,
			minings, this);
	cu.accept(visitor);
	return minings;
}
 
Example #16
Source File: JavaDebugElementCodeMiningASTVisitor.java    From jdt-codemining with Eclipse Public License 1.0 5 votes vote down vote up
public JavaDebugElementCodeMiningASTVisitor(IJavaStackFrame frame, CompilationUnit cu, ITextViewer viewer,
		List<ICodeMining> minings, AbstractDebugVariableCodeMiningProvider provider) {
	this.cu = cu;
	this.minings = minings;
	this.provider = provider;
	this.viewer = viewer;
	this.fFrame = frame;
}
 
Example #17
Source File: AbstractDebugVariableCodeMiningProvider.java    From jdt-codemining with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer,
		IProgressMonitor monitor) {
	return CompletableFuture.supplyAsync(() -> {
		monitor.isCanceled();
		addDebugListener(viewer);
		ITextEditor textEditor = super.getAdapter(ITextEditor.class);
		T stackFrame = getStackFrame(viewer, textEditor);
		if (stackFrame == null) {
			return Collections.emptyList();
		}
		return provideCodeMinings(viewer, stackFrame, monitor);
	});
}
 
Example #18
Source File: SARLCodeMiningProvider.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Add an annotation when the action's return type is implicit and inferred by the SARL compiler.
 *
 * @param resource the resource to parse.
 * @param acceptor the code mining acceptor.
 */
@SuppressWarnings("checkstyle:npathcomplexity")
private void createImplicitActionReturnType(XtextResource resource, IAcceptor<? super ICodeMining> acceptor) {
	final List<XtendFunction> actions = EcoreUtil2.eAllOfType(resource.getContents().get(0), XtendFunction.class);

	for (final XtendFunction action : actions) {
		// inline annotation only for methods with no return type
		if (action.getReturnType() != null) {
			continue;
		}
		// get return type name from operation
		final JvmOperation inferredOperation = (JvmOperation) this.jvmModelAssocitions.getPrimaryJvmElement(action);
		if (inferredOperation == null || inferredOperation.getReturnType() == null) {
			continue;
		}
		// find document offset for inline annotationn
		final ICompositeNode node = NodeModelUtils.findActualNodeFor(action);
		final Keyword parenthesis = this.grammar.getAOPMemberAccess().getRightParenthesisKeyword_2_5_6_2();
		final Assignment fctname = this.grammar.getAOPMemberAccess().getNameAssignment_2_5_5();
		int offsetFctname = -1;
		int offsetParenthesis = -1;
		for (Iterator<INode> it = node.getAsTreeIterable().iterator(); it.hasNext();) {
			final INode child = it.next();
			if (child != node) {
				final EObject grammarElement = child.getGrammarElement();
				if (grammarElement instanceof RuleCall) {
					if (fctname.equals(grammarElement.eContainer())) {
						offsetFctname = child.getTotalEndOffset();
					}
				} else if (parenthesis.equals(grammarElement)) {
					offsetParenthesis = child.getTotalEndOffset();
					break;
				}
			}
		}
		int offset = -1;
		if (offsetParenthesis >= 0) {
			offset = offsetParenthesis;
		} else if (offsetFctname >= 0) {
			offset = offsetFctname;
		}
		if (offset >= 0) {
			final String returnType = inferredOperation.getReturnType().getSimpleName();
			final String text = " " + this.keywords.getColonKeyword() + " " + returnType; //$NON-NLS-1$ //$NON-NLS-2$
			acceptor.accept(createNewLineContentCodeMining(offset, text));
		}
	}
}
 
Example #19
Source File: SARLCodeMiningProvider.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Add an annotation when the var/val declaration's type is implicit and inferred by the SARL compiler.
 *
 * @param resource the resource to parse.
 * @param acceptor the code mining acceptor.
 */
@SuppressWarnings({ "checkstyle:npathcomplexity" })
private <T extends EObject> void createImplicitVarValType(XtextResource resource, IAcceptor<? super ICodeMining> acceptor,
		Class<T> elementType,
		Function1<T, JvmTypeReference> getDeclaredTypeLambda,
		Function1<T, String> getInferredTypeLambda,
		Function1<T, XExpression> getExprLambda,
		Function0<Assignment> getAssignmentLambda) {
	final List<T> elements = EcoreUtil2.eAllOfType(resource.getContents().get(0), elementType);

	for (final T element : elements) {
		// inline annotation only for fields with no type
		final JvmTypeReference declaredType = getDeclaredTypeLambda.apply(element);
		final XExpression expr = getExprLambda != null ? getExprLambda.apply(element) : null;
		if (declaredType != null || (getExprLambda != null && expr == null)) {
			continue;
		}
		// get inferred type name from JVM element
		final String inferredType = getInferredTypeLambda.apply(element);
		if (Strings.isEmpty(inferredType)) {
			continue;
		}
		// find document offset for inline annotation
		final ICompositeNode node = NodeModelUtils.findActualNodeFor(element);
		final Assignment elementAssignment = getAssignmentLambda.apply();
		assert elementAssignment != null;
		for (Iterator<INode> it = node.getAsTreeIterable().iterator(); it.hasNext();) {
			final INode child = it.next();
			if (child != node) {
				final EObject grammarElement = child.getGrammarElement();
				if (grammarElement instanceof RuleCall) {
					if (elementAssignment.equals(grammarElement.eContainer())) {
						final String text = this.keywords.getColonKeyword() + " " + inferredType + " "; //$NON-NLS-1$ //$NON-NLS-2$
						final int offset = child.getPreviousSibling().getTotalOffset();
						acceptor.accept(createNewLineContentCodeMining(offset, text));
						break;
					}
				}
			}
		}
	}
}
 
Example #20
Source File: BalzacCodeMiningProvider.java    From balzac with Apache License 2.0 4 votes vote down vote up
@Override
protected void createCodeMinings(
    IDocument document,
    XtextResource resource,
    CancelIndicator indicator,
    IAcceptor<? super ICodeMining> acceptor) throws BadLocationException {

    // get all operations to open document
    List<Constant> allConstants = EcoreUtil2.eAllOfType(resource.getContents().get(0), Constant.class);

    Keyword colon = grammar.getConstantAccess().getColonKeyword_2_0();
    Keyword equalsSign = grammar.getConstantAccess().getEqualsSignKeyword_3();

    for (Constant c : allConstants) {
        // find document offset for inline annotation
        ICompositeNode node = NodeModelUtils.findActualNodeFor(c);

        boolean hasType = false;
        for (Iterator<INode> it = node.getAsTreeIterable().iterator(); it.hasNext();) {
            INode child = it.next();
            if (colon.equals(child.getGrammarElement())) {
                hasType = true;
            }
            if (!hasType && equalsSign.equals(child.getGrammarElement())) {
                // create line content code mining for inline annotation after grammarElement
                // ')'
                Result<Type> res = typeSystem.typeExpression(new TypeSubstitutions(), c);
                if (!res.failed()) {
                    String annotationText = ": " + strRep.stringRep(res.getFirst()) + " ";
                    acceptor.accept(createNewLineContentCodeMining(child.getTotalOffset(), annotationText));
                }
            }
        }
    }

    // TODO: implement me
    // use acceptor.accept(super.createNewLineHeaderCodeMining(...)) to add a new
    // code mining to the
    // final list

    // example:
    // acceptor.accept(createNewLineHeaderCodeMining(1, document, "Header
    // annotation"));
}
 
Example #21
Source File: AbstractXtextCodeMiningProvider.java    From xtext-eclipse with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Clients have to implement this method to provide code minings.
 * 
 * @param document The document
 * @param resource The resource for that document
 * @param indicator Cancelation indicator
 * @param acceptor Accepts created minings
 * @throws BadLocationException when line number doesn't exists
 */
protected abstract void createCodeMinings(IDocument document, XtextResource resource,
		CancelIndicator indicator, IAcceptor<? super ICodeMining> acceptor) throws BadLocationException;
 
Example #22
Source File: AbstractDebugVariableCodeMiningProvider.java    From jdt-codemining with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Collection minings included inside variables of the given stack frame.
 *
 * @param viewer
 * @param stackFrame
 * @param monitor
 * @return
 */
protected abstract List<? extends ICodeMining> provideCodeMinings(ITextViewer viewer, T stackFrame,
		IProgressMonitor monitor);