use of com.intellij.psi.PsiFile in project idea-handlebars by dmarcotte.
the class HbStructureViewFactory method getStructureViewBuilder.
@Nullable
@Override
public StructureViewBuilder getStructureViewBuilder(final PsiFile psiFile) {
return new TemplateLanguageStructureViewBuilder(psiFile) {
@Override
protected StructureViewComposite.StructureViewDescriptor createMainView(FileEditor fileEditor, PsiFile mainFile) {
if (!psiFile.isValid())
return null;
final StructureViewBuilder builder = new TreeBasedStructureViewBuilder() {
@NotNull
@Override
public StructureViewModel createStructureViewModel(@Nullable Editor editor) {
return new HbStructureViewModel((HbPsiFile) psiFile, editor);
}
};
StructureView structureView = builder.createStructureView(fileEditor, psiFile.getProject());
return new StructureViewComposite.StructureViewDescriptor(HbLanguage.INSTANCE.getDisplayName(), structureView, HbFileType.INSTANCE.getIcon());
}
};
}
use of com.intellij.psi.PsiFile in project idea-handlebars by dmarcotte.
the class HbsEmmetTest method testSimpleTagsWithHtmlSubstitutor.
public void testSimpleTagsWithHtmlSubstitutor() {
HbTestUtils.setOpenHtmlAsHandlebars(true, getProject(), getTestRootDisposable());
final PsiFile file = myFixture.configureByText("test.html", "div>span<caret>");
assertInstanceOf(file, HbPsiFile.class);
TemplateManager.getInstance(getProject()).startTemplate(myFixture.getEditor(), TemplateSettings.TAB_CHAR);
myFixture.checkResult("<div><span></span></div>");
}
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 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