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;
}
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));
}
}
}
};
}
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);
}
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"));
}
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);
}
}
Aggregations