use of com.intellij.lang.javascript.psi.JSFile in project intellij-plugins by JetBrains.
the class JSShortenFQNamesProcessor method processText.
public void processText(final Project project, final Template template, final Document document, final RangeMarker templateRange, final Editor editor) {
if (!template.isToShortenLongNames())
return;
try {
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
psiDocumentManager.commitDocument(document);
final PsiFile hostFile = PsiUtilBase.getPsiFileInEditor(editor, project);
final PsiFile file = (hostFile != null && JavaScriptSupportLoader.isFlexMxmFile(hostFile)) ? InjectedLanguageUtil.findInjectedPsiNoCommit(hostFile, templateRange.getStartOffset()) : hostFile;
if (file instanceof JSFile && file.getLanguage().isKindOf(JavaScriptSupportLoader.ECMA_SCRIPT_L4)) {
final ActionScriptUnusedImportsHelper.Results unusedImportsResults = ActionScriptUnusedImportsHelper.getUnusedImports(file);
for (final JSReferenceExpression reference : unusedImportsResults.fqnsToReplaceWithShortName) {
final TextRange range = InjectedLanguageManager.getInstance(project).injectedToHost(file, reference.getTextRange());
if (TextRange.create(templateRange).contains(range)) {
final String shortName = StringUtil.getShortName(reference.getReferencedName());
final String resolved = JSImportHandlingUtil.resolveTypeName(shortName, reference);
// insert import statement if needed
if (shortName.equals(resolved)) {
final FormatFixer fixer = ImportUtils.insertImportStatements(reference, Collections.singletonList(reference.getText()));
if (fixer != null) {
fixer.fixFormat();
}
}
// shorten FQN
reference.replace(JSChangeUtil.createExpressionFromText(project, shortName).getPsi());
}
}
psiDocumentManager.doPostponedOperationsAndUnblockDocument(document);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of com.intellij.lang.javascript.psi.JSFile in project intellij-plugins by JetBrains.
the class JstdRunProfileState method buildTestFileScope.
@NotNull
private static TestFileScope buildTestFileScope(@NotNull Project project, @NotNull JstdRunSettings settings) throws ExecutionException {
TestType testType = settings.getTestType();
if (testType == TestType.ALL_CONFIGS_IN_DIRECTORY || testType == TestType.CONFIG_FILE) {
return TestFileScope.allScope();
}
if (testType == TestType.JS_FILE) {
File jsFile = new File(settings.getJsFilePath());
if (jsFile.isAbsolute() && jsFile.isFile()) {
VirtualFile virtualFile = VfsUtil.findFileByIoFile(jsFile, true);
if (virtualFile != null) {
PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if (psiFile instanceof JSFile) {
JSFile jsPsiFile = (JSFile) psiFile;
TestFileStructurePack pack = TestFileStructureManager.fetchTestFileStructurePackByJsFile(jsPsiFile);
if (pack != null) {
List<String> testCases = pack.getTopLevelElements();
if (testCases.isEmpty()) {
throw new ExecutionException("No tests found in " + jsPsiFile.getName());
}
Map<String, Set<String>> scope = ContainerUtil.newHashMap();
for (String testCase : testCases) {
scope.put(testCase, Collections.emptySet());
}
return TestFileScope.customScope(scope);
}
}
}
}
throw new ExecutionException("Unable to extract tests from " + jsFile.getName());
}
if (testType == TestType.TEST_CASE) {
Map<String, Set<String>> scope = Collections.singletonMap(settings.getTestCaseName(), Collections.<String>emptySet());
return TestFileScope.customScope(scope);
}
if (testType == TestType.TEST_METHOD) {
Map<String, Set<String>> scope = Collections.singletonMap(settings.getTestCaseName(), Collections.singleton(settings.getTestMethodName()));
return TestFileScope.customScope(scope);
}
throw new RuntimeException("Unexpected test type: " + testType);
}
use of com.intellij.lang.javascript.psi.JSFile in project intellij-plugins by JetBrains.
the class JstdRunConfigurationVerifier method verifyTestCase.
@Nullable
private static TestFileStructurePack verifyTestCase(@NotNull Project project, @NotNull JstdRunSettings runSettings) throws RuntimeConfigurationException {
verifyJSFileType(runSettings);
if (runSettings.getTestCaseName().isEmpty()) {
throw new RuntimeConfigurationError("Test case name is empty.");
}
VirtualFile jsTestVirtualFile = VfsUtil.findFileByIoFile(new File(runSettings.getJsFilePath()), false);
if (jsTestVirtualFile == null) {
throw new RuntimeConfigurationWarning("Can't find JavaScript test file.");
}
JSFile jsFile = ObjectUtils.tryCast(PsiManager.getInstance(project).findFile(jsTestVirtualFile), JSFile.class);
if (jsFile == null) {
throw new RuntimeConfigurationWarning("Wrong JavaScript test file.");
}
TestFileStructurePack pack = TestFileStructureManager.fetchTestFileStructurePackByJsFile(jsFile);
if (pack != null) {
boolean found = pack.contains(runSettings.getTestCaseName(), null, JstdTestMethodNameRefiner.INSTANCE);
if (!found) {
throw new RuntimeConfigurationWarning("Can't find test case with name '" + runSettings.getTestCaseName() + "'.");
}
return pack;
}
return null;
}
use of com.intellij.lang.javascript.psi.JSFile in project intellij-plugins by JetBrains.
the class MxmlEnterHandler method preprocessEnter.
public Result preprocessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull Ref<Integer> caretOffset, @NotNull Ref<Integer> caretAdvance, @NotNull DataContext dataContext, EditorActionHandler originalHandler) {
int offset = caretOffset.get().intValue();
if (file instanceof JSFile) {
PsiElement context = InjectedLanguageManager.getInstance(file.getProject()).getInjectionHost(file);
if (context instanceof XmlComment) {
file = context.getContainingFile();
editor = ((EditorWindow) editor).getDelegate();
offset = editor.getCaretModel().getOffset();
}
}
if (!JavaScriptSupportLoader.isFlexMxmFile(file))
return Result.Continue;
if (CodeInsightSettings.getInstance().INSERT_BRACE_ON_ENTER && isAfterUnmatchedMxmlComment(editor, file, offset)) {
String indent = "";
CharSequence buffer = editor.getDocument().getCharsSequence();
int lineStart = CharArrayUtil.shiftBackwardUntil(buffer, offset - 1, "\n") + 1;
int current = lineStart;
while (current < offset && Character.isWhitespace(buffer.charAt(current))) ++current;
if (current > lineStart) {
indent = buffer.subSequence(lineStart, current).toString();
}
editor.getDocument().insertString(offset, "\n" + indent + "-->");
originalHandler.execute(editor, dataContext);
return Result.Stop;
}
return Result.Continue;
}
use of com.intellij.lang.javascript.psi.JSFile in project intellij-plugins by JetBrains.
the class FlexUtils method processMetaAttributesForClass.
public static void processMetaAttributesForClass(@NotNull PsiElement jsClass, @NotNull final ActionScriptResolveUtil.MetaDataProcessor processor) {
ActionScriptResolveUtil.processMetaAttributesForClass(jsClass, processor);
if (jsClass instanceof XmlBackedJSClassImpl) {
PsiElement parent = jsClass.getParent();
if (parent != null) {
PsiFile file = parent.getContainingFile();
if (file instanceof XmlFile) {
XmlDocument document = ((XmlFile) file).getDocument();
if (document != null) {
XmlTag rootTag = document.getRootTag();
if (rootTag != null) {
JSResolveUtil.JSInjectedFilesVisitor visitor = new JSResolveUtil.JSInjectedFilesVisitor() {
@Override
protected void process(JSFile file) {
if (file != null) {
ActionScriptResolveUtil.processMetaAttributesForClass(file, processor);
}
}
};
processMxmlTags(rootTag, true, visitor);
}
}
}
}
}
}
Aggregations