本文整理汇总了Java中com.intellij.psi.PsiFileFactory类的典型用法代码示例。如果您正苦于以下问题:Java PsiFileFactory类的具体用法?Java PsiFileFactory怎么用?Java PsiFileFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PsiFileFactory类属于com.intellij.psi包,在下文中一共展示了PsiFileFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: actionPerformed
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
@Override
public void actionPerformed(AnActionEvent anActionEvent)
{
final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(anActionEvent.getDataContext());
final Project project = anActionEvent.getProject();
Xsd2Raml xsd2Raml = new Xsd2Raml(file.getUrl());
String raml = xsd2Raml.getRaml();
PsiFile psiFile = PsiFileFactory.getInstance(anActionEvent.getProject()).createFileFromText(RamlLanguage.INSTANCE, raml);
new WriteCommandAction.Simple(project, psiFile) {
@Override
protected void run() throws Throwable {
psiFile.setName(file.getNameWithoutExtension() + "." + RamlFileType.INSTANCE.getDefaultExtension());
PsiDirectory directory = CommonDataKeys.PSI_FILE.getData(anActionEvent.getDataContext()).getContainingDirectory();
directory.add(psiFile);
}
}.execute();
}
示例2: createFromTemplate
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
public static PsiFile createFromTemplate(final PsiDirectory directory, final String name,
String fileName, String templateName,
@NonNls String... parameters) throws IncorrectOperationException {
final FileTemplate template = FileTemplateManager.getInstance(directory.getProject()).getInternalTemplate(templateName);
String text;
try {
text = template.getText();
} catch (Exception e) {
throw new RuntimeException("Unable to load template for " +
FileTemplateManager.getInstance().internalTemplateToSubject(templateName), e);
}
final PsiFileFactory factory = PsiFileFactory.getInstance(directory.getProject());
final PsiFile file = factory.createFileFromText(fileName, WeexFileType.INSTANCE, text);
CodeStyleManager.getInstance(directory.getProject()).reformat(file);
return (PsiFile) directory.add(file);
}
示例3: suggestKeywords
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
private static Collection<String> suggestKeywords(PsiElement position) {
TextRange posRange = position.getTextRange();
CupFile posFile = (CupFile) position.getContainingFile();
TextRange range = new TextRange(0, posRange.getStartOffset());
String text = range.isEmpty() ? CompletionInitializationContext.DUMMY_IDENTIFIER : range.substring(posFile.getText());
int completionOffset = posRange.getStartOffset() - range.getStartOffset(); // = posRange.getStartOffset() ...
PsiFile file = PsiFileFactory.getInstance(posFile.getProject()).createFileFromText("a.cup", CupLanguage.INSTANCE, text, true, false);
GeneratedParserUtilBase.CompletionState state = new GeneratedParserUtilBase.CompletionState(completionOffset) {
@Nullable
@Override
public String convertItem(Object o) {
if (o == CupTypes.IDENTIFIER) {
return null;
}
return o.toString();
}
};
file.putUserData(GeneratedParserUtilBase.COMPLETION_STATE_KEY, state);
TreeUtil.ensureParsed(file.getNode());
return state.items;
}
示例4: doRun
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
@Override
public void doRun() {
PsiDirectory psiParent = PsiHelper.findPsiDirByPath(project, file.getParentFile().getPath());
if(psiParent==null){
ReportHelper.setState(ExecutionState.FAILED);
return;
}
try {
PsiFile psiResultFile = PsiFileFactory.getInstance(project).createFileFromText(file.getName(), PlainTextFileType.INSTANCE, content);
// PsiFile psiFile = psiDirectory.createFile(psiDirectory.createFile(file.getName()).getName());
// psiFile.getVirtualFile().
psiParent.add(psiResultFile);
} catch (IncorrectOperationException ex){
Logger.log("CreateFileAction " + ex.getMessage());
Logger.printStack(ex);
ReportHelper.setState(ExecutionState.FAILED);
return;
}
}
示例5: handleContentChange
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
@Override
public FileReferenceNode handleContentChange(@NotNull FileReferenceNode fileReferenceNode,
@NotNull TextRange range, String newContent) throws IncorrectOperationException {
String oldText = fileReferenceNode.getText();
String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText
.substring(range.getEndOffset());
Project project = fileReferenceNode.getProject();
PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project);
IElementType type = ProtoParserDefinition.rule(ProtoParser.RULE_fileReference);
ScopeNode context = fileReferenceNode.getContext();
PsiElement newNode = factory
.createElementFromText(newText, ProtoLanguage.INSTANCE, type, context);
if (newNode == null) {
throw new IncorrectOperationException();
}
return (FileReferenceNode) fileReferenceNode.replace(newNode);
}
示例6: handleContentChange
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
@Override
public FieldReferenceNode handleContentChange(@NotNull FieldReferenceNode node,
@NotNull TextRange range, String newContent) throws IncorrectOperationException {
String oldText = node.getText();
String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText
.substring(range.getEndOffset());
Project project = node.getProject();
PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project);
IElementType type = ProtoParserDefinition.rule(ProtoParser.RULE_fieldRerefence);
ScopeNode context = node.getContext();
PsiElement newNode = factory
.createElementFromText(newText, ProtoLanguage.INSTANCE, type, context);
if (newNode == null) {
throw new IncorrectOperationException();
}
return (FieldReferenceNode) node.replace(newNode);
}
示例7: handleContentChange
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
@Override
public TypeReferenceNode handleContentChange(@NotNull TypeReferenceNode node,
@NotNull TextRange range, String newContent) throws IncorrectOperationException {
String oldText = node.getText();
String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText
.substring(range.getEndOffset());
Project project = node.getProject();
PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project);
IElementType type = ProtoParserDefinition.rule(ProtoParser.RULE_typeReference);
ScopeNode context = node.getContext();
PsiElement newNode = factory
.createElementFromText(newText, ProtoLanguage.INSTANCE, type, context);
if (newNode == null) {
throw new IncorrectOperationException();
}
return (TypeReferenceNode) node.replace(newNode);
}
示例8: setName
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
/**
* Set name.
*/
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
ASTNode node = getNode();
IElementType elementType = node.getElementType();
if (elementType instanceof RuleIElementType) {
RuleIElementType ruleElementType = (RuleIElementType) elementType;
int ruleIndex = ruleElementType.getRuleIndex();
Project project = getProject();
PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project);
IElementType type = ProtoParserDefinition.rule(ruleIndex);
ScopeNode context = getContext();
PsiElement newNode = factory.createElementFromText(name, ProtoLanguage.INSTANCE, type, context);
if (newNode == null) {
throw new IncorrectOperationException();
}
return replace(newNode);
}
throw new IncorrectOperationException(OPERATION_NOT_SUPPORTED);
}
示例9: createFromTemplate
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
public static PsiFile createFromTemplate(PsiDirectory directory, String fileName, String templateName) throws IncorrectOperationException {
final FileTemplate template = FileTemplateManager.getInstance(directory.getProject()).getInternalTemplate(templateName);
Project project = directory.getProject();
String text;
try {
text = template.getText();
} catch (Exception e) {
throw new RuntimeException("Unable to load template for " + FileTemplateManager.getInstance(project).internalTemplateToSubject(templateName), e);
}
final PsiFileFactory factory = PsiFileFactory.getInstance(project);
PsiFile file = factory.createFileFromText(fileName, NullShaderFileType.INSTANCE, text);
file = (PsiFile) directory.add(file);
return file;
}
示例10: doTextTest
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
protected void doTextTest(final String text, final String textAfter, @NotNull CheckPolicy checkPolicy)
throws IncorrectOperationException {
final String fileName = "before." + getFileExtension();
final PsiFile file = createFileFromText(text, fileName, PsiFileFactory.getInstance(getProject()));
if (checkPolicy.isCheckDocument()) {
checkDocument(file, text, textAfter);
}
if (checkPolicy.isCheckPsi()) {
/*
restoreFileContent(file, text);
checkPsi(file, textAfter);
*/
}
}
示例11: testNonPhysicalFile
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
public void testNonPhysicalFile() throws Exception {
String fileName = "Test.java";
FileType fileType = FileTypeRegistry.getInstance().getFileTypeByFileName(fileName);
PsiFile psiFile = PsiFileFactory.getInstance(getProject()).createFileFromText(fileName, fileType, "class Test {}", 0, false);
VirtualFile virtualFile = psiFile.getViewProvider().getVirtualFile();
Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
assertNotNull(document);
EditorFactory editorFactory = EditorFactory.getInstance();
Editor editor = editorFactory.createViewer(document, getProject());
try {
editor.getSelectionModel().setSelection(0, document.getTextLength());
String syntaxInfo = getSyntaxInfo(editor, psiFile);
assertEquals("foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=class \n" +
"foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=Test {}\n", syntaxInfo);
}
finally {
editorFactory.releaseEditor(editor);
}
}
示例12: parseText
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
private PsiElement parseText(String text) {
final Object source = getSource();
try {
if (source instanceof PsiViewerExtension) {
return ((PsiViewerExtension)source).createElement(myProject, text);
}
if (source instanceof FileType) {
final FileType type = (FileType)source;
String ext = type.getDefaultExtension();
if (myExtensionComboBox.isVisible()) {
ext = myExtensionComboBox.getSelectedItem().toString().toLowerCase();
}
if (type instanceof LanguageFileType) {
final Language dialect = (Language)myDialectComboBox.getSelectedItem();
if (dialect != null) {
return PsiFileFactory.getInstance(myProject).createFileFromText("Dummy." + ext, dialect, text);
}
}
return PsiFileFactory.getInstance(myProject).createFileFromText("Dummy." + ext, type, text);
}
}
catch (IncorrectOperationException e) {
Messages.showMessageDialog(myProject, e.getMessage(), "Error", Messages.getErrorIcon());
}
return null;
}
示例13: updateFileTypeForEditorComponent
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
private void updateFileTypeForEditorComponent(@NotNull ComboBox inputComboBox) {
final Component editorComponent = inputComboBox.getEditor().getEditorComponent();
if (editorComponent instanceof EditorTextField) {
boolean isRegexp = myCbRegularExpressions.isSelectedWhenSelectable();
FileType fileType = PlainTextFileType.INSTANCE;
if (isRegexp) {
Language regexpLanguage = Language.findLanguageByID("RegExp");
if (regexpLanguage != null) {
LanguageFileType regexpFileType = regexpLanguage.getAssociatedFileType();
if (regexpFileType != null) {
fileType = regexpFileType;
}
}
}
String fileName = isRegexp ? "a.regexp" : "a.txt";
final PsiFile file = PsiFileFactory.getInstance(myProject).createFileFromText(fileName, fileType, ((EditorTextField)editorComponent).getText(), -1, true);
((EditorTextField)editorComponent).setNewDocumentAndFileType(fileType, PsiDocumentManager.getInstance(myProject).getDocument(file));
}
}
示例14: createFromTemplate
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
@NotNull
@Override
public PsiElement createFromTemplate(final Project project, final PsiDirectory directory, String fileName, final FileTemplate template,
final String templateText,
@NotNull final Map<String, Object> props) throws IncorrectOperationException {
fileName = checkAppendExtension(fileName, template);
if (FileTypeManager.getInstance().isFileIgnored(fileName)) {
throw new IncorrectOperationException("This filename is ignored (Settings | Editor | File Types | Ignore files and folders)");
}
directory.checkCreateFile(fileName);
FileType type = FileTypeRegistry.getInstance().getFileTypeByFileName(fileName);
PsiFile file = PsiFileFactory.getInstance(project).createFileFromText(fileName, type, templateText);
if (template.isReformatCode()) {
CodeStyleManager.getInstance(project).reformat(file);
}
file = (PsiFile)directory.add(file);
return file;
}
示例15: getConfigPsiFile
import com.intellij.psi.PsiFileFactory; //导入依赖的package包/类
@Nullable
public BuildoutCfgFile getConfigPsiFile() {
File cfg = getConfigFile();
if (cfg != null && cfg.exists()) {
try {
// this method is called before the project initialization is complete, so it has to use createFileFromText() instead
// of PsiManager.findFile()
String text = FileUtil.loadFile(cfg);
final PsiFile configFile = PsiFileFactory
.getInstance(getModule().getProject()).createFileFromText("buildout.cfg",
BuildoutCfgLanguage.INSTANCE, text);
if (configFile != null && configFile instanceof BuildoutCfgFile) {
return (BuildoutCfgFile)configFile;
}
}
catch (Exception ignored) {
}
}
return null;
}