Search in sources :

Example 1 with CfmlMappingsConfig

use of com.intellij.coldFusion.UI.config.CfmlMappingsConfig in project intellij-plugins by JetBrains.

the class CfmlComponentReference method buildVariants.

@NotNull
public static Object[] buildVariants(String text, PsiFile containingFile, final Project project, @Nullable CfmlComponentReference reference, final boolean forceQualify) {
    Collection<Object> variants = new THashSet<>();
    String directoryName = "";
    if (text.contains(".")) {
        int i = text.lastIndexOf(".");
        directoryName = text.substring(0, i);
    }
    CfmlProjectConfiguration.State state = CfmlProjectConfiguration.getInstance(project).getState();
    CfmlMappingsConfig mappings = state != null ? state.getMapps().clone() : new CfmlMappingsConfig();
    adjustMappingsIfEmpty(mappings, project);
    if (reference != null)
        addFakeMappingsForImports(reference, mappings);
    List<String> realPossiblePaths = mappings.mapVirtualToReal(directoryName);
    for (String realPath : realPossiblePaths) {
        addVariantsFromPath(variants, directoryName, realPath);
    }
    for (String value : mappings.getServerMappings().keySet()) {
        if (value.startsWith(directoryName) && !value.isEmpty() && (StringUtil.startsWithChar(value, '/') || StringUtil.startsWithChar(value, '\\'))) {
            variants.add(value.replace('\\', '.').replace('/', '.').substring(1));
        }
    }
    containingFile = containingFile == null ? null : containingFile.getOriginalFile();
    if (containingFile != null && containingFile instanceof CfmlFile) {
        CfmlFile cfmlContainingFile = (CfmlFile) containingFile;
        if (directoryName.length() == 0) {
            PsiDirectory directory = cfmlContainingFile.getParent();
            if (directory != null) {
                addVariantsFromPath(variants, "", directory.getVirtualFile().getPresentableUrl());
            }
        } else {
            String dirPath = cfmlContainingFile.getParent().getVirtualFile().getPath() + "/" + directoryName.replaceAll("[./]+", "/");
            addVariantsFromPath(variants, directoryName, dirPath);
        }
    }
    final String finalDirectoryName = directoryName;
    return ContainerUtil.map2Array(variants, new Function<Object, Object>() {

        class DotInsertHandler implements InsertHandler<LookupElement> {

            @Override
            public void handleInsert(InsertionContext context, LookupElement item) {
                Document document = context.getDocument();
                int offset = context.getEditor().getCaretModel().getOffset();
                document.insertString(offset, ".");
                context.getEditor().getCaretModel().moveToOffset(offset + 1);
            }
        }

        public Object fun(final Object object) {
            if (object instanceof VirtualFile) {
                VirtualFile element = (VirtualFile) object;
                String elementNameWithoutExtension = element.getNameWithoutExtension();
                String name = forceQualify ? finalDirectoryName + (finalDirectoryName.length() == 0 ? "" : ".") + elementNameWithoutExtension : elementNameWithoutExtension;
                if (name.length() == 0)
                    name = element.getName();
                if (element.isDirectory()) {
                    return LookupElementBuilder.create(name).withIcon(DIRECTORY_CLOSED_ICON).withInsertHandler(new DotInsertHandler()).withCaseSensitivity(false);
                } else {
                    Icon icon = CLASS_ICON;
                    // choosing correct icon (class or interface)
                    if (CfmlIndex.getInstance(project).getComponentsByNameInScope(elementNameWithoutExtension, GlobalSearchScope.fileScope(project, element)).size() == 1) {
                        icon = CLASS_ICON;
                    } else if (CfmlIndex.getInstance(project).getInterfacesByNameInScope(elementNameWithoutExtension, GlobalSearchScope.fileScope(project, element)).size() == 1) {
                        icon = INTERFACE_ICON;
                    }
                    return LookupElementBuilder.create(name).withIcon(icon).withCaseSensitivity(false);
                }
            } else if (object instanceof String) {
                return LookupElementBuilder.create((String) object).withInsertHandler(new DotInsertHandler()).withCaseSensitivity(false);
            }
            return object;
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CfmlFile(com.intellij.coldFusion.model.files.CfmlFile) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) CfmlMappingsConfig(com.intellij.coldFusion.UI.config.CfmlMappingsConfig) CfmlProjectConfiguration(com.intellij.coldFusion.UI.config.CfmlProjectConfiguration) InsertHandler(com.intellij.codeInsight.completion.InsertHandler) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with CfmlMappingsConfig

use of com.intellij.coldFusion.UI.config.CfmlMappingsConfig in project intellij-plugins by JetBrains.

the class CfmlComponentReference method resolveFromQualifiedName.

/*
  private class PrefixSuffix {
    // with dot separators
    public String prefix;
    // with file separators
    public String suffix;

    private PrefixSuffix(String prefix, String suffix) {
      this.prefix = prefix;
      this.suffix = suffix;
    }
  }

  private List<String> getPossiblePathsForComponent(String componentQualifiedName) {
    CfmlMappingsConfig mappings = CfmlProjectConfiguration.getInstance(getProject()).getMappings();
    List<String> result = new LinkedList<String>();
    StringBuilder prefix = new StringBuilder();
    String suffix = File.separatorChar + componentQualifiedName.replace('.', File.separatorChar);

    List<PrefixSuffix> ps = new LinkedList<PrefixSuffix>();
    while (suffix != "") {
      ps.add(new PrefixSuffix(prefix.toString(), suffix));
      // recount prefix and suffix
      int i = suffix.indexOf(File.separatorChar, 1);
      if (i == -1) {
        break;
      }
      String s = suffix.substring(1, i);
      if (prefix.length() != 0) {
        s = File.separatorChar + s;
      }
      prefix.append(s);
      suffix = suffix.substring(i);
    }

    for (PrefixSuffix p : ps) {
      String s = mappings.serverMappings.get(p.prefix);
      if (s != null) {
        result.add(s + p.suffix);
      }
    }
    return result;
  }
  */
/**
   * @param componentQualifiedName
   * @param originalFile           = getContainingFile().getOriginalFile();
   * @return
   */
public static Collection<CfmlComponent> resolveFromQualifiedName(String componentQualifiedName, @NotNull CfmlFile originalFile) {
    List<CfmlComponent> result = new ArrayList<>();
    if (componentQualifiedName == null) {
        return result;
    }
    Project project = originalFile.getProject();
    if (!componentQualifiedName.contains(".")) {
        // resolve with directory scope
        // PsiFile containingFile = getContainingFile();
        // containingFile = containingFile == null ? null : containingFile.getOriginalFile();
        {
            CfmlFile cfmlConteiningFile = originalFile;
            PsiDirectory directory = cfmlConteiningFile.getParent();
            if (directory != null) {
                GlobalSearchScope searchScope = GlobalSearchScopes.directoryScope(directory, false);
                final Collection<CfmlComponent> components = CfmlIndex.getInstance(project).getComponentsByNameInScope(componentQualifiedName, searchScope);
                components.addAll(CfmlIndex.getInstance(project).getInterfacesByNameInScope(componentQualifiedName, searchScope));
                for (CfmlComponent component : components) {
                    result.add(component);
                }
            } else {
                final Collection<CfmlComponent> components = CfmlIndex.getInstance(project).getComponentsByName(componentQualifiedName);
                components.addAll(CfmlIndex.getInstance(project).getInterfacesByName(componentQualifiedName));
                for (CfmlComponent component : components) {
                    result.add(component);
                }
            }
        }
    }
    if (result.isEmpty()) {
        String componentName = getComponentName(componentQualifiedName);
        int i = componentQualifiedName.lastIndexOf(".");
        String directoryName;
        if (i == -1) {
            directoryName = "";
        } else {
            directoryName = componentQualifiedName.substring(0, i);
        }
        CfmlProjectConfiguration.State state = CfmlProjectConfiguration.getInstance(project).getState();
        CfmlMappingsConfig mappings = state != null ? state.getMapps().clone() : new CfmlMappingsConfig();
        adjustMappingsIfEmpty(mappings, originalFile.getProject());
        // addFakeMappingsForResolution(mappings);
        List<String> realPossiblePaths = mappings.mapVirtualToReal(directoryName);
        // Collections.sort(realPossiblePaths);
        final Collection<CfmlComponent> components = CfmlIndex.getInstance(project).getComponentsByName(componentName);
        components.addAll(CfmlIndex.getInstance(project).getInterfacesByName(componentName));
        for (CfmlComponent component : components) {
            PsiDirectory parent = component.getContainingFile().getParent();
            if (parent == null) {
                continue;
            }
            VirtualFile virtualFile = parent.getVirtualFile();
            for (String realPath : realPossiblePaths) {
                if (FileUtil.toSystemIndependentName(realPath).equals(FileUtil.toSystemIndependentName(virtualFile.getPresentableUrl()))) {
                    result.add(component);
                    break;
                }
            }
        }
        for (String realPath : realPossiblePaths) {
            VirtualFile fileByUrl = LocalFileSystem.getInstance().findFileByPath(realPath);
            if (fileByUrl != null) {
                PsiFile file = PsiManager.getInstance(project).findFile(fileByUrl);
                if (file != null) {
                    PsiDirectory directory = file.getParent();
                    if (directory != null) {
                        GlobalSearchScope searchScope = GlobalSearchScopes.directoryScope(directory, false);
                        final Collection<CfmlComponent> componentsFromGlobalScope = CfmlIndex.getInstance(project).getComponentsByNameInScope(componentName, searchScope);
                        componentsFromGlobalScope.addAll(CfmlIndex.getInstance(project).getInterfacesByNameInScope(componentName, searchScope));
                        for (CfmlComponent component : componentsFromGlobalScope) {
                            result.add(component);
                        }
                    }
                }
            }
        }
    }
    if (result.isEmpty()) {
        final Couple<String> prefixAndName = CfmlUtil.getPrefixAndName(componentQualifiedName);
        final String componentName = prefixAndName.getSecond();
        final CfmlImport cfmlImport = CfmlUtil.getImportByPrefix(originalFile, prefixAndName.getFirst());
        if (cfmlImport != null && !StringUtil.isEmpty(componentName)) {
            String libtag = cfmlImport.getImportString();
            final VirtualFile folder = CfmlUtil.findFileByLibTag(originalFile, libtag);
            if (folder != null && folder.isDirectory()) {
                final GlobalSearchScope scope = GlobalSearchScopes.directoryScope(originalFile.getProject(), folder, true);
                result.addAll(CfmlIndex.getInstance(originalFile.getProject()).getComponentsByNameInScope(componentName, scope));
            }
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CfmlFile(com.intellij.coldFusion.model.files.CfmlFile) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) CfmlMappingsConfig(com.intellij.coldFusion.UI.config.CfmlMappingsConfig) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Collection(java.util.Collection) CfmlProjectConfiguration(com.intellij.coldFusion.UI.config.CfmlProjectConfiguration)

Example 3 with CfmlMappingsConfig

use of com.intellij.coldFusion.UI.config.CfmlMappingsConfig in project intellij-plugins by JetBrains.

the class CfmlCompletionTest method testAutocompletePathToIncludeTagInPresentOfMappingWithFile.

public void testAutocompletePathToIncludeTagInPresentOfMappingWithFile() throws Throwable {
    addOneComponentToDir(myFixture);
    Map<String, String> mappings = new HashMap<>();
    for (VirtualFile root : ProjectRootManager.getInstance(getProject()).getContentRoots()) {
        String directoryName = root.getPresentableUrl() + "/subfolder";
        VirtualFile fileByUrl = LocalFileSystem.getInstance().findFileByPath(directoryName);
        if (fileByUrl != null) {
            mappings.put("/abc", directoryName);
        }
    }
    CfmlProjectConfiguration.State defaultState = CfmlProjectConfiguration.getInstance(getProject()).getState();
    CfmlProjectConfiguration.State state = new CfmlProjectConfiguration.State(new CfmlMappingsConfig(mappings));
    try {
        CfmlProjectConfiguration.getInstance(getProject()).loadState(state);
        doTestCompletionContainsVariants("ComponentName.cfc");
    } finally {
        CfmlProjectConfiguration.getInstance(getProject()).loadState(defaultState);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CfmlMappingsConfig(com.intellij.coldFusion.UI.config.CfmlMappingsConfig) HashMap(java.util.HashMap) CfmlProjectConfiguration(com.intellij.coldFusion.UI.config.CfmlProjectConfiguration)

Example 4 with CfmlMappingsConfig

use of com.intellij.coldFusion.UI.config.CfmlMappingsConfig in project intellij-plugins by JetBrains.

the class CfmlCompletionTest method testAutocompletePathToScriptIncludeInPresentOfMapping.

public void testAutocompletePathToScriptIncludeInPresentOfMapping() throws Throwable {
    addOneComponentTo(myFixture);
    Map<String, String> mappings = new HashMap<>();
    for (VirtualFile root : ProjectRootManager.getInstance(getProject()).getContentRoots()) {
        String directoryName = root.getPresentableUrl() + "/folder/subfolder";
        VirtualFile fileByUrl = LocalFileSystem.getInstance().findFileByPath(directoryName);
        if (fileByUrl != null) {
            mappings.put("/myfolder/subfolder", directoryName);
        }
    }
    CfmlProjectConfiguration.State defaultState = CfmlProjectConfiguration.getInstance(getProject()).getState();
    CfmlProjectConfiguration.State state = new CfmlProjectConfiguration.State(new CfmlMappingsConfig(mappings));
    try {
        CfmlProjectConfiguration.getInstance(getProject()).loadState(state);
        doTest();
    } finally {
        CfmlProjectConfiguration.getInstance(getProject()).loadState(defaultState);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CfmlMappingsConfig(com.intellij.coldFusion.UI.config.CfmlMappingsConfig) HashMap(java.util.HashMap) CfmlProjectConfiguration(com.intellij.coldFusion.UI.config.CfmlProjectConfiguration)

Example 5 with CfmlMappingsConfig

use of com.intellij.coldFusion.UI.config.CfmlMappingsConfig in project intellij-plugins by JetBrains.

the class CfmlCompletionTest method testNoCompletionToIncludeTagInPresentOfMapping.

public void testNoCompletionToIncludeTagInPresentOfMapping() throws Throwable {
    addOneComponentTo(myFixture);
    Map<String, String> mappings = new HashMap<>();
    for (VirtualFile root : ProjectRootManager.getInstance(getProject()).getContentRoots()) {
        String directoryName = root.getPresentableUrl() + "/folder";
        VirtualFile fileByUrl = LocalFileSystem.getInstance().findFileByPath(directoryName);
        if (fileByUrl != null) {
            mappings.put("myfolder", directoryName);
        }
    }
    CfmlProjectConfiguration.State defaultState = CfmlProjectConfiguration.getInstance(getProject()).getState();
    CfmlProjectConfiguration.State state = new CfmlProjectConfiguration.State(new CfmlMappingsConfig());
    state.setMapps(new CfmlMappingsConfig(mappings));
    try {
        CfmlProjectConfiguration.getInstance(getProject()).loadState(state);
        doTest();
    } finally {
        CfmlProjectConfiguration.getInstance(getProject()).loadState(defaultState);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CfmlMappingsConfig(com.intellij.coldFusion.UI.config.CfmlMappingsConfig) HashMap(java.util.HashMap) CfmlProjectConfiguration(com.intellij.coldFusion.UI.config.CfmlProjectConfiguration)

Aggregations

CfmlMappingsConfig (com.intellij.coldFusion.UI.config.CfmlMappingsConfig)13 CfmlProjectConfiguration (com.intellij.coldFusion.UI.config.CfmlProjectConfiguration)13 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 HashMap (java.util.HashMap)10 CfmlFile (com.intellij.coldFusion.model.files.CfmlFile)2 NotNull (org.jetbrains.annotations.NotNull)2 InsertHandler (com.intellij.codeInsight.completion.InsertHandler)1 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 FileReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 THashSet (gnu.trove.THashSet)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1