use of org.intellij.lang.annotations.Language in project kotlin by JetBrains.
the class KotlinIntegrationTestBase method normalizePath.
private static String normalizePath(String content, File baseDir, String pathId) {
String contentWithRelativePaths = content.replace(baseDir.getAbsolutePath(), pathId);
@Language("RegExp") String RELATIVE_PATH_WITH_MIXED_SEPARATOR = Regex.Companion.escape(pathId) + "[-.\\w/\\\\]*";
return new Regex(RELATIVE_PATH_WITH_MIXED_SEPARATOR).replace(contentWithRelativePaths, new Function1<MatchResult, String>() {
@Override
public String invoke(MatchResult mr) {
return FileUtil.toSystemIndependentName(mr.getValue());
}
});
}
use of org.intellij.lang.annotations.Language in project intellij-community by JetBrains.
the class JiraIntegrationTest method createIssueViaRestApi.
// We can use XML-RPC in JIRA 5+ too, but nonetheless it's useful to have REST-based implementation as well
private String createIssueViaRestApi(@NotNull String project, @NotNull String summary) throws Exception {
final HttpClient client = myRepository.getHttpClient();
final PostMethod method = new PostMethod(myRepository.getUrl() + "/rest/api/latest/issue");
try {
// For simplicity assume that project, summary and username don't contain illegal characters
@Language("JSON") final String json = "{\"fields\": {\n" + " \"project\": {\n" + " \"key\": \"" + project + "\"\n" + " },\n" + " \"issuetype\": {\n" + " \"name\": \"Bug\"\n" + " },\n" + " \"assignee\": {\n" + " \"name\": \"" + myRepository.getUsername() + "\"\n" + " },\n" + " \"summary\": \"" + summary + "\"\n" + "}}";
method.setRequestEntity(new StringRequestEntity(json, "application/json", "utf-8"));
client.executeMethod(method);
return new Gson().fromJson(method.getResponseBodyAsString(), JsonObject.class).get("id").getAsString();
} finally {
method.releaseConnection();
}
}
use of org.intellij.lang.annotations.Language in project intellij-community by JetBrains.
the class PsiDocumentManagerImplTest method testBackgroundCommitDoesNotChokeByWildChangesWhichInvalidatePsiFile.
public void testBackgroundCommitDoesNotChokeByWildChangesWhichInvalidatePsiFile() throws Exception {
@Language("JAVA") String text = "\n\nclass X {\npublic static final String string =null;\n public void x() {}\n}";
VirtualFile virtualFile = getVirtualFile(createTempFile("X.java", text));
PsiFile file = getPsiManager().findFile(virtualFile);
DocumentEx document = (DocumentEx) file.getViewProvider().getDocument();
PsiDocumentManager pdm = PsiDocumentManager.getInstance(myProject);
pdm.commitAllDocuments();
WriteCommandAction.runWriteCommandAction(null, () -> {
try {
virtualFile.setBinaryContent("\n txt txt txt".getBytes("UTF-8"));
virtualFile.rename(this, "X.txt");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
DocumentCommitThread.getInstance().waitForAllCommits();
assertTrue(pdm.isCommitted(document));
assertFalse(file.isValid());
WriteCommandAction.runWriteCommandAction(null, () -> document.replaceString(0, document.getTextLength(), "xxxxxxxxxxxxxxxxxxxx"));
pdm.commitAllDocuments();
assertTrue(pdm.isCommitted(document));
PsiFile file2 = getPsiManager().findFile(virtualFile);
assertEquals(PlainTextLanguage.INSTANCE, file2.getLanguage());
}
use of org.intellij.lang.annotations.Language in project android by JetBrains.
the class ResourceNotificationManagerTest method test.
public void test() {
@Language("XML") String xml;
// Setup sample project: a strings file, and a couple of layout file
xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<FrameLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + " android:layout_width=\"match_parent\"\n" + " android:layout_height=\"match_parent\">\n" + " <!-- My comment -->\n" + " <TextView " + " android:layout_width=\"match_parent\"\n" + " android:layout_height=\"match_parent\"\n" + " android:text=\"@string/hello\" />\n" + "</FrameLayout>";
final XmlFile layout1 = (XmlFile) myFixture.addFileToProject("res/layout/my_layout1.xml", xml);
@SuppressWarnings("ConstantConditions") VirtualFile resourceDir = layout1.getParent().getParent().getVirtualFile();
assertNotNull(resourceDir);
xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + " android:layout_width=\"match_parent\"\n" + " android:layout_height=\"match_parent\" />\n";
final XmlFile layout2 = (XmlFile) myFixture.addFileToProject("res/layout/my_layout2.xml", xml);
xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + " <string name=\"hello\">Hello</string>\n" + "\n" + " <!-- Base application theme. -->\n" + " <style name=\"AppTheme\" parent=\"Theme.AppCompat.Light.DarkActionBar\">\n" + " <!-- Customize your theme here. -->\n" + " <item name=\"android:colorBackground\">#ff0000</item>\n" + " </style>" + "</resources>";
final XmlFile values1 = (XmlFile) myFixture.addFileToProject("res/values/my_values1.xml", xml);
xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n" + " \n" + "</resources>";
myFixture.addFileToProject("res/values/colors.xml", xml);
final Configuration configuration1 = myFacet.getConfigurationManager().getConfiguration(layout1.getVirtualFile());
final ResourceNotificationManager manager = ResourceNotificationManager.getInstance(getProject());
// Listener 1: Listens for changes in layout 1
final Ref<Boolean> called1 = new Ref<>(false);
final Ref<Set<Reason>> calledValue1 = new Ref<>();
ResourceChangeListener listener1 = new ResourceChangeListener() {
@Override
public void resourcesChanged(@NotNull Set<Reason> reason) {
called1.set(true);
calledValue1.set(reason);
}
};
// Listener 2: Only listens for general changes in the module
final Ref<Boolean> called2 = new Ref<>(false);
final Ref<Set<Reason>> calledValue2 = new Ref<>();
ResourceChangeListener listener2 = new ResourceChangeListener() {
@Override
public void resourcesChanged(@NotNull Set<Reason> reason) {
called2.set(true);
calledValue2.set(reason);
}
};
manager.addListener(listener1, myFacet, layout1, configuration1);
manager.addListener(listener2, myFacet, null, null);
// Make sure that when we're modifying multiple files, with complicated
// edits (that trigger full file rescans), we handle that scenario correctly.
clear(called1, calledValue1, called2, calledValue2);
// There's actually some special optimizations done via PsiResourceItem#recomputeValue
// to only mark the resource repository changed if the value has actually been looked
// up. This allows us to not recompute layout if you're editing some string that
// hasn't actually been looked up and rendered in a layout. In order to make sure
// that that optimization doesn't kick in here, we need to look up the value of
// the resource item first:
//noinspection ConstantConditions
assertEquals("#ff0000", configuration1.getResourceResolver().getStyle("AppTheme", false).getItem("colorBackground", true).getValue());
AndroidResourceUtil.createValueResource(getProject(), resourceDir, "color2", ResourceType.COLOR, "colors.xml", Collections.singletonList("values"), "#fa2395");
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
clear(called1, calledValue1, called2, calledValue2);
@SuppressWarnings("ConstantConditions") final XmlTag tag = values1.getDocument().getRootTag().getSubTags()[1].getSubTags()[0];
assertEquals("item", tag.getName());
WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {
@Override
public void run() {
tag.getValue().setEscapedText("@color/color2");
}
});
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
// First check: Modify the layout by changing @string/hello to @string/hello_world
// and verify that our listeners are called.
ResourceVersion version1 = manager.getCurrentVersion(myFacet, layout1, configuration1);
addText(layout1, "@string/hello^", "_world");
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.EDIT);
ResourceVersion version2 = manager.getCurrentVersion(myFacet, layout1, configuration1);
assertFalse(version1.toString(), version1.equals(version2));
// Next check: Modify a <string> value definition in a values file
// and check that those changes are flagged too
clear(called1, calledValue1, called2, calledValue2);
ResourceVersion version3 = manager.getCurrentVersion(myFacet, layout1, configuration1);
addText(values1, "name=\"hello^\"", "_world");
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
ResourceVersion version4 = manager.getCurrentVersion(myFacet, layout1, configuration1);
assertFalse(version4.toString(), version3.equals(version4));
// Next check: Modify content in a comment and verify that no changes are fired
clear(called1, calledValue1, called2, calledValue2);
addText(layout1, "My ^comment", "new ");
ensureNotCalled(called1, called2);
// Check that editing text in a layout file has no effect
clear(called1, calledValue1, called2, calledValue2);
addText(layout1, " ^ <TextView", "abc");
ensureNotCalled(called1, called2);
// Make sure that's true for replacements too
replaceText(layout1, "^abc", "abc".length(), "def");
ensureNotCalled(called1, called2);
// ...and for deletions
removeText(layout1, "^def", "def".length());
ensureNotCalled(called1, called2);
// Check that editing text in a *values file* -does- have an effect
// Read the value first to ensure that we trigger it as a read (see comment above for previous
// resource resolver lookup)
//noinspection ConstantConditions
assertEquals("Hello", configuration1.getResourceResolver().findResValue("@string/hello_world", false).getValue());
addText(values1, "Hello^</string>", " World");
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
// Check that recreating AppResourceRepository object doesn't affect the ResourceNotificationManager
clear(called1, calledValue1, called2, calledValue2);
myFacet.refreshResources();
AndroidResourceUtil.createValueResource(getProject(), resourceDir, "color4", ResourceType.COLOR, "colors.xml", Collections.singletonList("values"), "#ff2300");
ensureCalled(called1, calledValue1, called2, calledValue2, Reason.RESOURCE_EDIT);
// Finally check that once we remove the listeners there are no more notifications
manager.removeListener(listener1, myFacet, layout1, configuration1);
manager.removeListener(listener2, myFacet, layout2, configuration1);
clear(called1, calledValue1, called2, calledValue2);
addText(layout1, "@string/hello_world^", "2");
ensureNotCalled(called1, called2);
// TODO: Check that editing a partial URL doesn't re-render
// Check module dependency triggers!
// TODO: Test that remove and replace editing also works as expected
}
use of org.intellij.lang.annotations.Language in project android by JetBrains.
the class ResourceTypeInspectionTest method getEnvironmentClasses.
@Override
protected String[] getEnvironmentClasses() {
@Language("JAVA") String header = "package android.support.annotation;\n" + "\n" + "import java.lang.annotation.Documented;\n" + "import java.lang.annotation.Retention;\n" + "import java.lang.annotation.Target;\n" + "\n" + "import static java.lang.annotation.ElementType.ANNOTATION_TYPE;\n" + "import static java.lang.annotation.ElementType.CONSTRUCTOR;\n" + "import static java.lang.annotation.ElementType.FIELD;\n" + "import static java.lang.annotation.ElementType.LOCAL_VARIABLE;\n" + "import static java.lang.annotation.ElementType.METHOD;\n" + "import static java.lang.annotation.ElementType.PARAMETER;\n" + "import static java.lang.annotation.ElementType.TYPE;\n" + "import static java.lang.annotation.RetentionPolicy.SOURCE;\n" + "import static java.lang.annotation.RetentionPolicy.CLASS;\n" + "\n";
List<String> classes = Lists.newArrayList();
@Language("JAVA") String floatRange = "@Retention(CLASS)\n" + "@Target({CONSTRUCTOR,METHOD,PARAMETER,FIELD,LOCAL_VARIABLE})\n" + "public @interface FloatRange {\n" + " double from() default Double.NEGATIVE_INFINITY;\n" + " double to() default Double.POSITIVE_INFINITY;\n" + " boolean fromInclusive() default true;\n" + " boolean toInclusive() default true;\n" + "}";
classes.add(header + floatRange);
@Language("JAVA") String intRange = "@Retention(CLASS)\n" + "@Target({CONSTRUCTOR,METHOD,PARAMETER,FIELD,LOCAL_VARIABLE,ANNOTATION_TYPE})\n" + "public @interface IntRange {\n" + " long from() default Long.MIN_VALUE;\n" + " long to() default Long.MAX_VALUE;\n" + "}";
classes.add(header + intRange);
@Language("JAVA") String size = "@Retention(CLASS)\n" + "@Target({PARAMETER, LOCAL_VARIABLE, METHOD, FIELD})\n" + "public @interface Size {\n" + " long value() default -1;\n" + " long min() default Long.MIN_VALUE;\n" + " long max() default Long.MAX_VALUE;\n" + " long multiple() default 1;\n" + "}";
classes.add(header + size);
@Language("JAVA") String permission = "@Retention(SOURCE)\n" + "@Target({ANNOTATION_TYPE,METHOD,CONSTRUCTOR,FIELD})\n" + "public @interface RequiresPermission {\n" + " String value() default \"\";\n" + " String[] allOf() default {};\n" + " String[] anyOf() default {};\n" + " boolean conditional() default false;\n" + " @Target(FIELD)\n" + " @interface Read {\n" + " RequiresPermission value();\n" + " }\n" + " @Target(FIELD)\n" + " @interface Write {\n" + " RequiresPermission value();\n" + " }\n" + "}\n";
classes.add(header + permission);
@Language("JAVA") String uiThread = "@Retention(SOURCE)\n" + "@Target({METHOD,CONSTRUCTOR,TYPE})\n" + "public @interface UiThread {\n" + "}";
classes.add(header + uiThread);
@Language("JAVA") String mainThread = "@Retention(SOURCE)\n" + "@Target({METHOD,CONSTRUCTOR,TYPE})\n" + "public @interface MainThread {\n" + "}";
classes.add(header + mainThread);
@Language("JAVA") String workerThread = "@Retention(SOURCE)\n" + "@Target({METHOD,CONSTRUCTOR,TYPE})\n" + "public @interface WorkerThread {\n" + "}";
classes.add(header + workerThread);
@Language("JAVA") String binderThread = "@Retention(SOURCE)\n" + "@Target({METHOD,CONSTRUCTOR,TYPE})\n" + "public @interface BinderThread {\n" + "}";
classes.add(header + binderThread);
@Language("JAVA") String colorInt = "@Retention(SOURCE)\n" + "@Target({METHOD, PARAMETER, FIELD, LOCAL_VARIABLE})\n" + "public @interface ColorInt {\n" + "}";
classes.add(header + colorInt);
@Language("JAVA") String px = "@Retention(SOURCE)\n" + "@Target({METHOD, PARAMETER, FIELD, LOCAL_VARIABLE})\n" + "public @interface Px {\n" + "}";
classes.add(header + px);
@Language("JAVA") String dimension = "@Retention(SOURCE)\n" + "@Target({METHOD,PARAMETER,FIELD,LOCAL_VARIABLE,ANNOTATION_TYPE})\n" + "public @interface Dimension {\n" + " @DimensionUnit\n" + " int unit() default PX;\n" + " int DP = 0;\n" + " int PX = 1;\n" + " int SP = 2;\n" + "}";
classes.add(header + dimension);
@Language("JAVA") String requiresApi = "@Retention(SOURCE)\n" + "@Target({TYPE,METHOD,CONSTRUCTOR,FIELD})\n" + "public @interface RequiresApi {\n" + " int value();\n" + "}";
classes.add(header + requiresApi);
@Language("JAVA") String intDef = "@Retention(SOURCE)\n" + "@Target({ANNOTATION_TYPE})\n" + "public @interface IntDef {\n" + " long[] value() default {};\n" + " boolean flag() default false;\n" + "}\n";
classes.add(header + intDef);
@Language("JAVA") String stringDef = "@Retention(SOURCE)\n" + "@Target({ANNOTATION_TYPE})\n" + "public @interface StringDef {\n" + " String[] value() default {};\n" + "}\n";
classes.add(header + stringDef);
for (ResourceType type : ResourceType.values()) {
if (type == ResourceType.FRACTION || type == ResourceType.PUBLIC) {
continue;
}
@Language("JAVA") String resourceTypeAnnotation = "@Documented\n" + "@Retention(SOURCE)\n" + "@Target({METHOD, PARAMETER, FIELD})\n" + "public @interface " + StringUtil.capitalize(type.getName()) + "Res {\n" + "}";
classes.add(header + resourceTypeAnnotation);
}
String anyRes = "@Documented\n" + "@Retention(SOURCE)\n" + "@Target({METHOD, PARAMETER, FIELD})\n" + "public @interface AnyRes {\n" + "}";
classes.add(header + anyRes);
@Language("JAVA") String unrelatedThread = "package some.pkg;\n" + "import java.lang.annotation.*;\n" + "import static java.lang.annotation.ElementType.*;\n" + "import static java.lang.annotation.RetentionPolicy.*;\n" + "@Retention(SOURCE)\n" + "@Target({METHOD,CONSTRUCTOR,TYPE})\n" + "public @interface UnrelatedNameEndsWithThread {\n" + "}";
classes.add(unrelatedThread);
@Language("JAVA") String checkResult = "@Retention(SOURCE)\n" + "@Target({METHOD})\n" + "public @interface CheckResult {\n" + "}\n";
classes.add(header + checkResult);
@Language("JAVA") String restrictTo = "@Retention(CLASS)\n" + "@Target({ANNOTATION_TYPE,TYPE,METHOD,CONSTRUCTOR,FIELD,PACKAGE})\n" + "public @interface RestrictTo {\n" + " Scope[] value();\n" + " enum Scope {\n" + " GROUP_ID,\n" + " TESTS,\n" + " SUBCLASSES\n" + " }\n" + "}\n";
classes.add(header + restrictTo);
return ArrayUtil.toStringArray(classes);
}
Aggregations