use of com.intellij.psi.PsiFile in project idea-handlebars by dmarcotte.
the class HbFormatterTest method doFormatterActionTest.
private void doFormatterActionTest(final FormatRunnableFactory formatAction, final String beforeText, String textAfter, LanguageFileType templateDataLanguageType) {
PsiFile baseFile = myFixture.configureByText("A.hbs", beforeText);
VirtualFile virtualFile = baseFile.getVirtualFile();
assert virtualFile != null;
TemplateDataLanguageMappings.getInstance(getProject()).setMapping(virtualFile, templateDataLanguageType.getLanguage());
// fetch a fresh instance of the file -- the template data mapping creates a new instance,
// which was causing problems in PsiFileImpl.isValid()
final PsiFile file = PsiManager.getInstance(getProject()).findFile(virtualFile);
assert file != null;
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(formatAction.createFormatRunnable(file));
}
}, "", "");
TemplateDataLanguageMappings.getInstance(getProject()).cleanupForNextTest();
assertEquals("Reformat Code failed", prepareText(textAfter), prepareText(file.getText()));
assertEquals("Reformat Code failed", prepareText(textAfter), prepareText(file.getText()));
}
use of com.intellij.psi.PsiFile in project idea-handlebars by dmarcotte.
the class HbFormatterTest method doTextTest.
/**
* This method runs both a full-file reformat on beforeText, and a line-by-line reformat. Though the tests
* would output slightly better errors if these were separate tests, enforcing that they are always both run
* for any test defined is the easiest way to ensure that the line-by-line is not messed up by formatter changes
*
* @param beforeText The text run the formatter on
* @param textAfter The expected result after running the formatter
* @param templateDataLanguageType The templated language of the file
* @throws IncorrectOperationException
*/
void doTextTest(final String beforeText, String textAfter, LanguageFileType templateDataLanguageType) throws IncorrectOperationException {
// define action to run "Reformat Code" on the whole "file" defined by beforeText
FormatRunnableFactory fullFormatRunnableFactory = new FormatRunnableFactory() {
@Override
Runnable createFormatRunnable(final PsiFile file) {
return new Runnable() {
@Override
public void run() {
try {
TextRange rangeToUse = file.getTextRange();
CodeStyleManager styleManager = CodeStyleManager.getInstance(getProject());
styleManager.reformatText(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
} catch (IncorrectOperationException e) {
assertTrue(e.getLocalizedMessage(), false);
}
}
};
}
};
// define action to run "Adjust line indent" on every line in the "file" defined by beforeText
FormatRunnableFactory lineFormatRunnableFactory = new FormatRunnableFactory() {
@Override
Runnable createFormatRunnable(final PsiFile file) {
return new Runnable() {
@Override
public void run() {
try {
final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
final Document document = manager.getDocument(file);
assert document != null;
for (int lineNum = 0; lineNum < document.getLineCount(); lineNum++) {
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
int offset = document.getLineStartOffset(lineNum);
// if this breaks at some point, we should
@SuppressWarnings("deprecation") boolean // instead of doing the indent directly
lineToBeIndented = codeStyleManager.isLineToBeIndented(file, offset);
if (lineToBeIndented) {
codeStyleManager.adjustLineIndent(file, offset);
}
}
} catch (IncorrectOperationException e) {
assertTrue(e.getLocalizedMessage(), false);
}
}
};
}
};
doFormatterActionTest(fullFormatRunnableFactory, beforeText, textAfter, templateDataLanguageType);
doFormatterActionTest(lineFormatRunnableFactory, beforeText, textAfter, templateDataLanguageType);
}
use of com.intellij.psi.PsiFile in project buck by facebook.
the class BuckAutoDepsContributor method getVirtualFileFromClassname.
@Nullable
public VirtualFile getVirtualFileFromClassname(String classname) {
GlobalSearchScope scope = GlobalSearchScope.allScope(mProject);
PsiClass psiClass = JavaPsiFacade.getInstance(mProject).findClass(classname, scope);
if (psiClass == null) {
return null;
}
PsiFile psiFile = psiClass.getContainingFile();
if (psiFile == null) {
return null;
}
return psiFile.getVirtualFile();
}
use of com.intellij.psi.PsiFile in project android-parcelable-intellij-plugin by mcharmas.
the class ParcelableAction method getPsiClassFromContext.
private PsiClass getPsiClassFromContext(AnActionEvent e) {
PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
Editor editor = e.getData(PlatformDataKeys.EDITOR);
if (psiFile == null || editor == null) {
return null;
}
int offset = editor.getCaretModel().getOffset();
PsiElement element = psiFile.findElementAt(offset);
return PsiTreeUtil.getParentOfType(element, PsiClass.class);
}
use of com.intellij.psi.PsiFile in project folding-plugin by dmytrodanylyk.
the class ProjectStructureProvider method createComposedFiles.
@NotNull
private List<AbstractTreeNode> createComposedFiles(@NotNull Collection<AbstractTreeNode> fileNodes, ViewSettings viewSettings) {
List<AbstractTreeNode> resultList = new ArrayList<>();
Project project = Utils.getCurrentProject();
if (project != null) {
HashSet<String> composedDirNameSet = new HashSet<>();
List<AbstractTreeNode> notComposedFileNodes = new ArrayList<>();
final boolean customPrefix = PropertiesComponent.getInstance().getBoolean(SettingConfigurable.PREFIX_CUSTOM_USE, false);
Pattern pattern = customPrefix ? Pattern.compile(PropertiesComponent.getInstance().getValue(SettingConfigurable.PREFIX_PATTERN, SettingConfigurable.DEFAULT_PATTERN)) : null;
for (AbstractTreeNode fileNode : fileNodes) {
if (fileNode.getValue() instanceof PsiFile) {
PsiFile psiFile = (PsiFile) fileNode.getValue();
String fileName = psiFile.getName();
if (customPrefix) {
Matcher m = pattern.matcher(fileName);
if (m.find()) {
String composedDirName = m.group(0);
composedDirNameSet.add(composedDirName);
} else {
notComposedFileNodes.add(fileNode);
}
} else {
int endIndex = fileName.indexOf(COMPOSE_BY_CHAR);
if (endIndex != -1) {
String composedDirName = fileName.substring(0, endIndex);
composedDirNameSet.add(composedDirName);
} else {
notComposedFileNodes.add(fileNode);
}
}
}
}
for (String composedDirName : composedDirNameSet) {
List<AbstractTreeNode> composedFileNodes = filterByDirName(fileNodes, composedDirName);
PsiFile psiFile = (PsiFile) composedFileNodes.get(0).getValue();
DirectoryNode composedDirNode = new DirectoryNode(project, viewSettings, psiFile, composedDirName);
composedDirNode.addAllChildren(composedFileNodes);
resultList.add(composedDirNode);
}
if (!notComposedFileNodes.isEmpty()) {
PsiFile psiFile = (PsiFile) notComposedFileNodes.get(0).getValue();
DirectoryNode composedDirNode = new DirectoryNode(project, viewSettings, psiFile, OTHER_NODE);
composedDirNode.addAllChildren(notComposedFileNodes);
resultList.add(composedDirNode);
}
}
return resultList;
}
Aggregations