use of com.intellij.psi.PsiFile in project Main by SpartanRefactoring.
the class SpartanizerAction method getPsiElementFromContext.
/**
* @param e the action event
* @return the psiElement extracted from the event's context
**/
@Nullable
private PsiElement getPsiElementFromContext(AnActionEvent e) {
PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
Editor editor = e.getData(PlatformDataKeys.EDITOR);
return psiFile == null || editor == null ? null : psiFile.findElementAt(editor.getCaretModel().getOffset());
}
use of com.intellij.psi.PsiFile in project Main by SpartanRefactoring.
the class SpartanizerAnnotator method annotate.
@Override
public void annotate(@NotNull final PsiElement e, @NotNull AnnotationHolder h) {
try {
if (!Spartanizer.canTip(e) || e.getContainingFile().getName().contains("Spartanizer"))
return;
Annotation annotation = h.createInfoAnnotation(e, "Spartanize This!");
annotation.registerFix(new IntentionAction() {
@SuppressWarnings("unchecked")
@Nls
@NotNull
@Override
public String getText() {
return Toolbox.getInstance().getTipper(e).description(e);
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return "SpartanizerAction";
}
@Override
public boolean isAvailable(@NotNull Project __, Editor e, PsiFile f) {
return true;
}
@Override
public void invoke(@NotNull Project p, Editor ed, PsiFile f) throws IncorrectOperationException {
Spartanizer.spartanizeElement(e);
}
@Override
public boolean startInWriteAction() {
return false;
}
});
TextAttributesKey.createTextAttributesKey("");
annotation.setEnforcedTextAttributes((new TextAttributes(null, null, JBColor.BLUE, EffectType.WAVE_UNDERSCORE, 0)));
} catch (Throwable t) {
Logger l = new Logger(this.getClass());
l.error("", t);
}
}
use of com.intellij.psi.PsiFile in project Main by SpartanRefactoring.
the class Playground method spartanizeButtonClicked.
private void spartanizeButtonClicked() {
if (inputArea.getText().trim().isEmpty()) {
return;
}
Toolbox.getInstance().playground = true;
PsiFile file = null;
final boolean[] worked = { true };
int i;
for (i = 0; worked[0] && i < before.length; i++, worked[0] = true) {
file = PsiFileFactory.getInstance(Utils.getProject()).createFileFromText(JavaLanguage.INSTANCE, before[i] + inputArea.getText() + after[i]);
file.accept(new JavaRecursiveElementVisitor() {
@Override
public void visitErrorElement(PsiErrorElement element) {
worked[0] = false;
super.visitErrorElement(element);
}
});
if (worked[0]) {
break;
}
}
if (i < before.length && file != null) {
Spartanizer.spartanizeFileOnePass(file);
outputArea.setText(fixString(file.getText(), i));
} else {
outputArea.setText(inputArea.getText());
}
Toolbox.getInstance().playground = false;
}
use of com.intellij.psi.PsiFile in project kotlin by JetBrains.
the class AbstractFormatterTest method doTextTest.
public void doTextTest(final Action action, final String text, File fileAfter, String extension) throws IncorrectOperationException {
final PsiFile file = createFile("A" + extension, text);
if (myLineRange != null) {
DocumentImpl document = new DocumentImpl(text);
myTextRange = new TextRange(document.getLineStartOffset(myLineRange.getStartOffset()), document.getLineEndOffset(myLineRange.getEndOffset()));
}
final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
final Document document = manager.getDocument(file);
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
document.replaceString(0, document.getTextLength(), text);
manager.commitDocument(document);
try {
TextRange rangeToUse = myTextRange;
if (rangeToUse == null) {
rangeToUse = file.getTextRange();
}
ACTIONS.get(action).run(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
} catch (IncorrectOperationException e) {
assertTrue(e.getLocalizedMessage(), false);
}
}
});
}
}, "", "");
if (document == null) {
fail("Don't expect the document to be null");
return;
}
KotlinTestUtils.assertEqualsToFile(fileAfter, document.getText());
manager.commitDocument(document);
KotlinTestUtils.assertEqualsToFile(fileAfter, file.getText());
}
use of com.intellij.psi.PsiFile in project kotlin by JetBrains.
the class JavaParser method getLocation.
/**
* Returns a {@link Location} for the given element
*
* @param context information about the file being parsed
* @param element the element to create a location for
* @return a location for the given node
*/
// subclasses may want to override/optimize
@SuppressWarnings("MethodMayBeStatic")
@NonNull
public Location getLocation(@NonNull JavaContext context, @NonNull PsiElement element) {
TextRange range = element.getTextRange();
UFile uFile = (UFile) getUastContext().convertElementWithParent(element.getContainingFile(), UFile.class);
if (uFile == null) {
return Location.NONE;
}
PsiFile containingFile = uFile.getPsi();
File file = context.file;
if (containingFile != context.getUFile().getPsi()) {
// Reporting an error in a different file.
if (context.getDriver().getScope().size() == 1) {
// Don't bother with this error if it's in a different file during single-file analysis
return Location.NONE;
}
VirtualFile virtualFile = containingFile.getVirtualFile();
if (virtualFile == null) {
return Location.NONE;
}
file = VfsUtilCore.virtualToIoFile(virtualFile);
}
return Location.create(file, context.getContents(), range.getStartOffset(), range.getEndOffset());
}
Aggregations