Search in sources :

Example 6 with Header

use of org.jetbrains.lang.manifest.psi.Header in project intellij-plugins by JetBrains.

the class OsgiPsiUtil method createHeader.

private static Header createHeader(Project project, String headerName, String valueText) {
    String text = String.format("%s: %s\n", headerName, valueText);
    PsiFile file = PsiFileFactory.getInstance(project).createFileFromText("DUMMY.MF", ManifestFileTypeFactory.MANIFEST, text);
    Header header = ((ManifestFile) file).getHeader(headerName);
    if (header == null) {
        throw new IncorrectOperationException("Bad header: '" + text + "'");
    }
    return header;
}
Also used : Header(org.jetbrains.lang.manifest.psi.Header) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ManifestFile(org.jetbrains.lang.manifest.psi.ManifestFile)

Example 7 with Header

use of org.jetbrains.lang.manifest.psi.Header in project intellij-community by JetBrains.

the class MisspelledHeaderInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PsiElementVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof Header) {
                Header header = (Header) element;
                String headerName = header.getName();
                SortedSet<Suggestion> matches = new TreeSet<>();
                addMatches(headerName, CUSTOM_HEADERS, matches);
                addMatches(headerName, myRepository.getAllHeaderNames(), matches);
                Suggestion bestMatch = ContainerUtil.getFirstItem(matches);
                if (bestMatch != null && headerName.equals(bestMatch.getWord())) {
                    return;
                }
                List<LocalQuickFix> fixes = new ArrayList<>();
                for (Suggestion match : matches) {
                    fixes.add(new HeaderRenameQuickFix(header, match.getWord()));
                    if (fixes.size() == MAX_SUGGESTIONS)
                        break;
                }
                if (bestMatch == null || bestMatch.getMetrics() > TYPO_DISTANCE) {
                    fixes.add(new CustomHeaderQuickFix(header, CUSTOM_HEADERS));
                }
                holder.registerProblem(header.getNameElement(), ManifestBundle.message("inspection.header.message"), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, fixes.toArray(new LocalQuickFix[fixes.size()]));
            }
        }

        private void addMatches(String headerName, Collection<String> headers, SortedSet<Suggestion> matches) {
            for (String candidate : headers) {
                int distance = EditDistance.optimalAlignment(headerName, candidate, false);
                if (distance <= MAX_DISTANCE) {
                    matches.add(new Suggestion(candidate, distance));
                }
            }
        }
    };
}
Also used : LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) Suggestion(com.intellij.spellchecker.engine.Suggestion) Header(org.jetbrains.lang.manifest.psi.Header) AbstractCollection(com.intellij.util.xmlb.annotations.AbstractCollection) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with Header

use of org.jetbrains.lang.manifest.psi.Header in project intellij-plugins by JetBrains.

the class OsgiManifestPsiTest method testBundleVersion.

public void testBundleVersion() {
    ManifestFile file = createFile("Bundle-Version: 1.2.3.b300\n");
    Header header = file.getHeader("Bundle-Version");
    assertNotNull(header);
    Object value = HeaderParserRepository.getInstance().getConvertedValue(header);
    assertEquals(new Version(1, 2, 3, "b300"), value);
}
Also used : Header(org.jetbrains.lang.manifest.psi.Header) Version(org.osgi.framework.Version) ManifestFile(org.jetbrains.lang.manifest.psi.ManifestFile)

Example 9 with Header

use of org.jetbrains.lang.manifest.psi.Header in project intellij-plugins by JetBrains.

the class OsgiManifestPsiTest method testClauses.

public void testClauses() {
    ManifestFile file = createFile("Import-Package: a.b,c.d;a=value;d:=value");
    Header header = file.getHeader("Import-Package");
    assertNotNull(header);
    List<HeaderValue> clauses = header.getHeaderValues();
    assertEquals(2, clauses.size());
    Clause clause1 = (Clause) clauses.get(0), clause2 = (Clause) clauses.get(1);
    assertEquals(0, clause1.getAttributes().size());
    assertEquals(0, clause1.getDirectives().size());
    assertEquals(1, clause2.getAttributes().size());
    assertEquals(1, clause2.getDirectives().size());
    assertNotNull(clause2.getAttribute("a"));
    assertNull(clause2.getAttribute("b"));
    assertNotNull(clause2.getDirective("d"));
    assertNull(clause2.getDirective("z"));
}
Also used : HeaderValue(org.jetbrains.lang.manifest.psi.HeaderValue) Header(org.jetbrains.lang.manifest.psi.Header) Clause(org.osmorc.manifest.lang.psi.Clause) ManifestFile(org.jetbrains.lang.manifest.psi.ManifestFile)

Example 10 with Header

use of org.jetbrains.lang.manifest.psi.Header in project intellij-plugins by JetBrains.

the class OsgiManifestPsiTest method assertAssignment.

private static void assertAssignment(ManifestFile file, boolean attribute, String name, String expected) {
    List<Header> headers = file.getHeaders();
    assertEquals(1, headers.size());
    List<HeaderValue> clauses = headers.get(0).getHeaderValues();
    assertEquals(1, clauses.size());
    assertTrue(clauses.get(0) instanceof Clause);
    Clause clause = (Clause) clauses.get(0);
    AssignmentExpression element = attribute ? clause.getAttribute(name) : clause.getDirective(name);
    if (expected != null) {
        assertNotNull(element);
        assertEquals(expected, element.getValue());
    } else {
        assertNull(element);
    }
}
Also used : HeaderValue(org.jetbrains.lang.manifest.psi.HeaderValue) Header(org.jetbrains.lang.manifest.psi.Header) AssignmentExpression(org.osmorc.manifest.lang.psi.AssignmentExpression) Clause(org.osmorc.manifest.lang.psi.Clause)

Aggregations

Header (org.jetbrains.lang.manifest.psi.Header)11 HeaderValue (org.jetbrains.lang.manifest.psi.HeaderValue)4 ManifestFile (org.jetbrains.lang.manifest.psi.ManifestFile)4 PsiElement (com.intellij.psi.PsiElement)2 Clause (org.osmorc.manifest.lang.psi.Clause)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)1 PsiReference (com.intellij.psi.PsiReference)1 Suggestion (com.intellij.spellchecker.engine.Suggestion)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 AbstractCollection (com.intellij.util.xmlb.annotations.AbstractCollection)1 NotNull (org.jetbrains.annotations.NotNull)1 HeaderParser (org.jetbrains.lang.manifest.header.HeaderParser)1 Section (org.jetbrains.lang.manifest.psi.Section)1 Version (org.osgi.framework.Version)1 AssignmentExpression (org.osmorc.manifest.lang.psi.AssignmentExpression)1