Search in sources :

Example 6 with JavaCodeFragment

use of com.intellij.psi.JavaCodeFragment in project smali by JesusFreke.

the class SmaliCodeFragmentFactory method createPresentationCodeFragment.

@Override
public JavaCodeFragment createPresentationCodeFragment(TextWithImports item, PsiElement context, Project project) {
    context = wrapContext(project, context);
    JavaCodeFragment fragment = super.createPresentationCodeFragment(item, context, project);
    List<LazyValue> lazyValues = context.getUserData(SMALI_LAZY_VALUES_KEY);
    if (lazyValues != null) {
        fragment.putUserData(SMALI_LAZY_VALUES_KEY, lazyValues);
    }
    return fragment;
}
Also used : LazyValue(org.jf.smalidea.debugging.value.LazyValue) JavaCodeFragment(com.intellij.psi.JavaCodeFragment)

Example 7 with JavaCodeFragment

use of com.intellij.psi.JavaCodeFragment in project smali by JesusFreke.

the class SmaliCodeFragmentFactoryTest method assertCompletionContains.

private void assertCompletionContains(String completionText, PsiElement context, String[] expectedItems, String[] disallowedItems) {
    SmaliCodeFragmentFactory codeFragmentFactory = new SmaliCodeFragmentFactory();
    JavaCodeFragment fragment = codeFragmentFactory.createCodeFragment(new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, completionText), context, getProject());
    Editor editor = createEditor(fragment.getVirtualFile());
    editor.getCaretModel().moveToOffset(completionText.length());
    new CodeCompletionHandlerBase(CompletionType.BASIC).invokeCompletion(getProject(), editor);
    List<LookupElement> elements = LookupManager.getInstance(getProject()).getActiveLookup().getItems();
    HashSet expectedSet = Sets.newHashSet(expectedItems);
    HashSet disallowedSet = Sets.newHashSet(disallowedItems);
    for (LookupElement element : elements) {
        expectedSet.remove(element.toString());
        Assert.assertFalse(disallowedSet.contains(element.toString()));
    }
    Assert.assertTrue(expectedSet.size() == 0);
}
Also used : SmaliCodeFragmentFactory(org.jf.smalidea.debugging.SmaliCodeFragmentFactory) TextWithImportsImpl(com.intellij.debugger.engine.evaluation.TextWithImportsImpl) CodeCompletionHandlerBase(com.intellij.codeInsight.completion.CodeCompletionHandlerBase) Editor(com.intellij.openapi.editor.Editor) LookupElement(com.intellij.codeInsight.lookup.LookupElement) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) HashSet(java.util.HashSet)

Example 8 with JavaCodeFragment

use of com.intellij.psi.JavaCodeFragment in project intellij-community by JetBrains.

the class JavaExtractor method getDiffer.

@NotNull
@Override
public Differ getDiffer(final Project project, PsiFile psiFile, CodeStyleSettings settings) {
    return new DifferBase(project, psiFile, settings) {

        @Override
        public String reformattedText() {
            JavaCodeFragment file = JavaCodeFragmentFactory.getInstance(project).createCodeBlockCodeFragment(myOrigText, myFile, false);
            WriteCommandAction.runWriteCommandAction(myProject, "CodeStyleSettings extractor", "CodeStyleSettings extractor", () -> {
                ASTNode treeElement = SourceTreeToPsiMap.psiToTreeNotNull(file);
                SourceTreeToPsiMap.treeElementToPsi(new CodeFormatterFacade(mySettings, file.getLanguage()).processElement(treeElement));
            }, file);
            return file.getText();
        }
    };
}
Also used : ASTNode(com.intellij.lang.ASTNode) CodeFormatterFacade(com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with JavaCodeFragment

use of com.intellij.psi.JavaCodeFragment in project intellij-community by JetBrains.

the class CodeFragmentFactoryContextWrapper method wrapContext.

private PsiElement wrapContext(Project project, final PsiElement originalContext) {
    if (project.isDefault())
        return originalContext;
    //TODO [egor] : does not work for anything other than java anyway, see IDEA-132677
    if (!(myDelegate instanceof DefaultCodeFragmentFactory)) {
        return originalContext;
    }
    PsiElement context = originalContext;
    XDebugSession session = XDebuggerManager.getInstance(project).getCurrentSession();
    if (session != null) {
        XValueMarkers<?, ?> markers = ((XDebugSessionImpl) session).getValueMarkers();
        Map<?, ValueMarkup> markupMap = markers != null ? markers.getAllMarkers() : null;
        //final Map<ObjectReference, ValueMarkup> markupMap = ValueDescriptorImpl.getMarkupMap(process);
        if (!ContainerUtil.isEmpty(markupMap)) {
            final Pair<String, Map<String, ObjectReference>> markupVariables = createMarkupVariablesText(markupMap);
            int offset = markupVariables.getFirst().length() - 1;
            final TextWithImportsImpl textWithImports = new TextWithImportsImpl(CodeFragmentKind.CODE_BLOCK, markupVariables.getFirst(), "", myDelegate.getFileType());
            final JavaCodeFragment codeFragment = myDelegate.createCodeFragment(textWithImports, context, project);
            codeFragment.accept(new JavaRecursiveElementVisitor() {

                public void visitLocalVariable(PsiLocalVariable variable) {
                    final String name = variable.getName();
                    variable.putUserData(LABEL_VARIABLE_VALUE_KEY, markupVariables.getSecond().get(name));
                }
            });
            final PsiElement newContext = codeFragment.findElementAt(offset);
            if (newContext != null) {
                context = newContext;
            }
        }
    }
    return context;
}
Also used : XDebugSession(com.intellij.xdebugger.XDebugSession) PsiLocalVariable(com.intellij.psi.PsiLocalVariable) ValueMarkup(com.intellij.xdebugger.impl.ui.tree.ValueMarkup) HashMap(java.util.HashMap) Map(java.util.Map) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) JavaRecursiveElementVisitor(com.intellij.psi.JavaRecursiveElementVisitor) PsiElement(com.intellij.psi.PsiElement) XDebugSessionImpl(com.intellij.xdebugger.impl.XDebugSessionImpl)

Aggregations

JavaCodeFragment (com.intellij.psi.JavaCodeFragment)9 PsiElement (com.intellij.psi.PsiElement)5 LazyValue (org.jf.smalidea.debugging.value.LazyValue)3 TextWithImportsImpl (com.intellij.debugger.engine.evaluation.TextWithImportsImpl)2 Editor (com.intellij.openapi.editor.Editor)2 JavaRecursiveElementVisitor (com.intellij.psi.JavaRecursiveElementVisitor)2 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)2 SmaliCodeFragmentFactory (org.jf.smalidea.debugging.SmaliCodeFragmentFactory)2 CodeCompletionHandlerBase (com.intellij.codeInsight.completion.CodeCompletionHandlerBase)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 ASTNode (com.intellij.lang.ASTNode)1 JavaCodeFragmentFactory (com.intellij.psi.JavaCodeFragmentFactory)1 PsiClass (com.intellij.psi.PsiClass)1 PsiCodeFragment (com.intellij.psi.PsiCodeFragment)1 PsiType (com.intellij.psi.PsiType)1 CodeFormatterFacade (com.intellij.psi.impl.source.codeStyle.CodeFormatterFacade)1 PsiReferenceExpressionImpl (com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl)1 ExtractLightMethodObjectHandler (com.intellij.refactoring.extractMethodObject.ExtractLightMethodObjectHandler)1 XDebugSession (com.intellij.xdebugger.XDebugSession)1 XDebugSessionImpl (com.intellij.xdebugger.impl.XDebugSessionImpl)1