Search in sources :

Example 1 with CompletionService

use of com.intellij.codeInsight.completion.CompletionService in project intellij-community by JetBrains.

the class DefaultCodeFragmentFactory method createCodeFragment.

public JavaCodeFragment createCodeFragment(TextWithImports item, PsiElement context, final Project project) {
    final JavaCodeFragmentFactory factory = JavaCodeFragmentFactory.getInstance(project);
    final String text = item.getText();
    final JavaCodeFragment fragment;
    if (CodeFragmentKind.EXPRESSION == item.getKind()) {
        final String expressionText = StringUtil.endsWithChar(text, ';') ? text.substring(0, text.length() - 1) : text;
        fragment = factory.createExpressionCodeFragment(expressionText, context, null, true);
    } else /*if (CodeFragmentKind.CODE_BLOCK == item.getKind())*/
    {
        fragment = factory.createCodeBlockCodeFragment(text, context, true);
    }
    if (item.getImports().length() > 0) {
        fragment.addImportsFromString(item.getImports());
    }
    fragment.setVisibilityChecker(JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE);
    //noinspection HardCodedStringLiteral
    fragment.putUserData(KEY, "DebuggerComboBoxEditor.IS_DEBUGGER_EDITOR");
    fragment.putCopyableUserData(JavaCompletionUtil.DYNAMIC_TYPE_EVALUATOR, (expression, parameters) -> {
        if (!RuntimeTypeEvaluator.isSubtypeable(expression)) {
            return null;
        }
        if (parameters.getInvocationCount() <= 1 && JavaCompletionUtil.mayHaveSideEffects(expression)) {
            final CompletionService service = CompletionService.getCompletionService();
            if (parameters.getInvocationCount() < 2) {
                service.setAdvertisementText("Invoke completion once more to see runtime type variants");
            }
            return null;
        }
        final DebuggerContextImpl debuggerContext = DebuggerManagerEx.getInstanceEx(project).getContext();
        DebuggerSession debuggerSession = debuggerContext.getDebuggerSession();
        if (debuggerSession != null && debuggerContext.getSuspendContext() != null) {
            final Semaphore semaphore = new Semaphore();
            semaphore.down();
            final AtomicReference<PsiType> nameRef = new AtomicReference<>();
            final RuntimeTypeEvaluator worker = new RuntimeTypeEvaluator(null, expression, debuggerContext, ProgressManager.getInstance().getProgressIndicator()) {

                @Override
                protected void typeCalculationFinished(@Nullable PsiType type) {
                    nameRef.set(type);
                    semaphore.up();
                }
            };
            debuggerSession.getProcess().getManagerThread().invoke(worker);
            for (int i = 0; i < 50; i++) {
                ProgressManager.checkCanceled();
                if (semaphore.waitFor(20))
                    break;
            }
            return nameRef.get();
        }
        return null;
    });
    return fragment;
}
Also used : RuntimeTypeEvaluator(com.intellij.debugger.codeinsight.RuntimeTypeEvaluator) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(com.intellij.util.concurrency.Semaphore) DebuggerSession(com.intellij.debugger.impl.DebuggerSession) CompletionService(com.intellij.codeInsight.completion.CompletionService) DebuggerContextImpl(com.intellij.debugger.impl.DebuggerContextImpl) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CompletionService (com.intellij.codeInsight.completion.CompletionService)1 RuntimeTypeEvaluator (com.intellij.debugger.codeinsight.RuntimeTypeEvaluator)1 DebuggerContextImpl (com.intellij.debugger.impl.DebuggerContextImpl)1 DebuggerSession (com.intellij.debugger.impl.DebuggerSession)1 Semaphore (com.intellij.util.concurrency.Semaphore)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Nullable (org.jetbrains.annotations.Nullable)1