use of com.intellij.psi.PsiJavaFile in project intellij-community by JetBrains.
the class EnterInJavadocParamDescriptionHandler method postProcessEnter.
@Override
public Result postProcessEnter(@NotNull final PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) {
if (!(file instanceof PsiJavaFile) || !CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER || !CodeStyleSettingsManager.getSettings(file.getProject()).JD_ALIGN_PARAM_COMMENTS) {
return Result.Continue;
}
final CaretModel caretModel = editor.getCaretModel();
final int caretOffset = caretModel.getOffset();
if (!isInJavaDoc(editor, caretOffset)) {
return Result.Continue;
}
final Pair<JavadocHelper.JavadocParameterInfo, List<JavadocHelper.JavadocParameterInfo>> pair = myHelper.parse(file, editor, caretOffset);
if (pair.first == null || pair.first.parameterDescriptionStartPosition == null) {
return Result.Continue;
}
final LogicalPosition caretPosition = caretModel.getLogicalPosition();
final LogicalPosition nameEndPosition = pair.first.parameterNameEndPosition;
if (nameEndPosition.line == caretPosition.line && caretPosition.column <= nameEndPosition.column) {
return Result.Continue;
}
final int descriptionStartColumn = pair.first.parameterDescriptionStartPosition.column;
final LogicalPosition desiredPosition = new LogicalPosition(caretPosition.line, descriptionStartColumn);
final Document document = editor.getDocument();
final CharSequence text = document.getCharsSequence();
final int offsetAfterLastWs = CharArrayUtil.shiftForward(text, caretOffset, " \t");
if (editor.offsetToLogicalPosition(offsetAfterLastWs).column < desiredPosition.column) {
final int lineStartOffset = document.getLineStartOffset(desiredPosition.line);
final String toInsert = StringUtil.repeat(" ", desiredPosition.column - (offsetAfterLastWs - lineStartOffset));
ApplicationManager.getApplication().runWriteAction(() -> {
document.insertString(caretOffset, toInsert);
PsiDocumentManager.getInstance(file.getProject()).commitDocument(document);
});
}
myHelper.navigate(desiredPosition, editor, file.getProject());
return Result.Stop;
}
use of com.intellij.psi.PsiJavaFile in project intellij-community by JetBrains.
the class FileClassMacro method expand.
public String expand(DataContext dataContext) {
//Project project = (Project)dataContext.getData(DataConstants.PROJECT);
//if (project == null) {
// return null;
//}
//VirtualFile file = (VirtualFile)dataContext.getData(DataConstantsEx.VIRTUAL_FILE);
//if (file == null) {
// return null;
//}
//PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
//if (!(psiFile instanceof PsiJavaFile)) {
// return null;
//}
final PsiFile javaFile = CommonDataKeys.PSI_FILE.getData(dataContext);
if (!(javaFile instanceof PsiJavaFile))
return null;
PsiClass[] classes = ((PsiJavaFile) javaFile).getClasses();
if (classes.length == 1) {
return classes[0].getQualifiedName();
}
String fileName = javaFile.getVirtualFile().getNameWithoutExtension();
for (PsiClass aClass : classes) {
String name = aClass.getName();
if (fileName.equals(name)) {
return aClass.getQualifiedName();
}
}
return null;
}
use of com.intellij.psi.PsiJavaFile in project kotlin by JetBrains.
the class JavaPsiVisitor method visitFile.
void visitFile(@NonNull final JavaContext context) {
try {
final PsiJavaFile javaFile = mParser.parseJavaToPsi(context);
if (javaFile == null) {
// with details, location, etc.
return;
}
try {
context.setJavaFile(javaFile);
mParser.runReadAction(new Runnable() {
@Override
public void run() {
for (VisitingDetector v : mAllDetectors) {
v.setContext(context);
v.getDetector().beforeCheckFile(context);
}
}
});
if (!mSuperClassDetectors.isEmpty()) {
mParser.runReadAction(new Runnable() {
@Override
public void run() {
SuperclassPsiVisitor visitor = new SuperclassPsiVisitor(context);
javaFile.accept(visitor);
}
});
}
for (final VisitingDetector v : mFullTreeDetectors) {
mParser.runReadAction(new Runnable() {
@Override
public void run() {
JavaElementVisitor visitor = v.getVisitor();
javaFile.accept(visitor);
}
});
}
if (!mMethodDetectors.isEmpty() || !mResourceFieldDetectors.isEmpty() || !mConstructorDetectors.isEmpty() || !mReferenceDetectors.isEmpty()) {
mParser.runReadAction(new Runnable() {
@Override
public void run() {
// TODO: Do we need to break this one up into finer grain
// locking units
JavaElementVisitor visitor = new DelegatingPsiVisitor(context);
javaFile.accept(visitor);
}
});
} else {
if (!mNodePsiTypeDetectors.isEmpty()) {
mParser.runReadAction(new Runnable() {
@Override
public void run() {
// TODO: Do we need to break this one up into finer grain
// locking units
JavaElementVisitor visitor = new DispatchPsiVisitor();
javaFile.accept(visitor);
}
});
}
}
mParser.runReadAction(new Runnable() {
@Override
public void run() {
for (VisitingDetector v : mAllDetectors) {
v.getDetector().afterCheckFile(context);
}
}
});
} finally {
mParser.dispose(context, javaFile);
context.setJavaFile(null);
}
} catch (ProcessCanceledException ignore) {
// Cancelling inspections in the IDE
} catch (RuntimeException e) {
if (sExceptionCount++ > MAX_REPORTED_CRASHES) {
// are tripping up ECJ, they get the picture.
return;
}
if (e.getClass().getSimpleName().equals("IndexNotReadyException")) {
// See http://b.android.com/176644 for an example.
return;
}
// Work around ECJ bugs; see https://code.google.com/p/android/issues/detail?id=172268
// Don't allow lint bugs to take down the whole build. TRY to log this as a
// lint error instead!
StringBuilder sb = new StringBuilder(100);
sb.append("Unexpected failure during lint analysis of ");
sb.append(context.file.getName());
sb.append(" (this is a bug in lint or one of the libraries it depends on)\n");
sb.append(e.getClass().getSimpleName());
sb.append(':');
StackTraceElement[] stackTrace = e.getStackTrace();
int count = 0;
for (StackTraceElement frame : stackTrace) {
if (count > 0) {
sb.append("<-");
}
String className = frame.getClassName();
sb.append(className.substring(className.lastIndexOf('.') + 1));
sb.append('.').append(frame.getMethodName());
sb.append('(');
sb.append(frame.getFileName()).append(':').append(frame.getLineNumber());
sb.append(')');
count++;
// Only print the top 3-4 frames such that we can identify the bug
if (count == 4) {
break;
}
}
// NOT e: this makes for very noisy logs
Throwable throwable = null;
//noinspection ConstantConditions
context.log(throwable, sb.toString());
}
}
use of com.intellij.psi.PsiJavaFile in project intellij-plugins by JetBrains.
the class ActivatorRenameTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
PsiFile activatorFile = myFixture.addFileToProject("src/t1/Activator.java", "package t1;\n\n" + "import org.osgi.framework.*;\n\n" + "public class Activator implements BundleActivator {\n" + " public void start(BundleContext context) throws Exception { }\n" + " public void stop(BundleContext context) throws Exception { }\n" + "}\n");
myActivator = ((PsiJavaFile) activatorFile).getClasses()[0];
}
Aggregations