use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class ShowExpressionTypeHandler method invoke.
public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull PsiFile file) {
ApplicationManager.getApplication().assertIsDispatchThread();
Language language = PsiUtilCore.getLanguageAtOffset(file, editor.getCaretModel().getOffset());
final Set<ExpressionTypeProvider> handlers = getHandlers(project, language, file.getViewProvider().getBaseLanguage());
if (handlers.isEmpty())
return;
boolean exactRange = false;
TextRange range = EditorUtil.getSelectionInAnyMode(editor);
final Map<PsiElement, ExpressionTypeProvider> map = ContainerUtil.newLinkedHashMap();
int offset = TargetElementUtil.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset());
for (int i = 0; i < 3 && map.isEmpty() && offset > i; i++) {
PsiElement elementAt = file.findElementAt(offset - i);
if (elementAt == null)
continue;
for (ExpressionTypeProvider handler : handlers) {
for (PsiElement element : ((ExpressionTypeProvider<? extends PsiElement>) handler).getExpressionsAt(elementAt)) {
TextRange r = element.getTextRange();
if (exactRange && !r.equals(range) || !r.contains(range))
continue;
if (!exactRange)
exactRange = r.equals(range);
map.put(element, handler);
}
}
}
Pass<PsiElement> callback = new Pass<PsiElement>() {
@Override
public void pass(@NotNull PsiElement expression) {
//noinspection unchecked
ExpressionTypeProvider<PsiElement> provider = ObjectUtils.assertNotNull(map.get(expression));
final String informationHint = provider.getInformationHint(expression);
TextRange range = expression.getTextRange();
editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
ApplicationManager.getApplication().invokeLater(() -> HintManager.getInstance().showInformationHint(editor, informationHint));
}
};
if (map.isEmpty()) {
ApplicationManager.getApplication().invokeLater(() -> {
String errorHint = ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(handlers)).getErrorHint();
HintManager.getInstance().showErrorHint(editor, errorHint);
});
} else if (map.size() == 1) {
callback.pass(ObjectUtils.assertNotNull(ContainerUtil.getFirstItem(map.keySet())));
} else {
IntroduceTargetChooser.showChooser(editor, ContainerUtil.newArrayList(map.keySet()), callback, expression -> expression.getText());
}
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class OfflineDescriptorResolveResult method getElementsIntersectingRange.
@NotNull
private static PsiElement[] getElementsIntersectingRange(PsiFile file, final int startOffset, final int endOffset) {
final FileViewProvider viewProvider = file.getViewProvider();
final Set<PsiElement> result = new LinkedHashSet<>();
for (Language language : viewProvider.getLanguages()) {
final PsiFile psiRoot = viewProvider.getPsi(language);
if (HighlightingLevelManager.getInstance(file.getProject()).shouldInspect(psiRoot)) {
result.addAll(CollectHighlightsUtil.getElementsInRange(psiRoot, startOffset, endOffset, true));
}
}
return PsiUtilCore.toPsiElementArray(result);
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class GotoClassAction method gotoActionPerformed.
@Override
public void gotoActionPerformed(@NotNull AnActionEvent e) {
final Project project = e.getProject();
if (project == null)
return;
FeatureUsageTracker.getInstance().triggerFeatureUsed("navigation.popup.class");
PsiDocumentManager.getInstance(project).commitAllDocuments();
final GotoClassModel2 model = new GotoClassModel2(project);
showNavigationPopup(e, model, new GotoActionCallback<Language>() {
@Override
protected ChooseByNameFilter<Language> createFilter(@NotNull ChooseByNamePopup popup) {
return new ChooseByNameLanguageFilter(popup, model, GotoClassSymbolConfiguration.getInstance(project), project);
}
@Override
public void elementChosen(ChooseByNamePopup popup, Object element) {
ApplicationManager.getApplication().runReadAction(() -> {
if (element instanceof PsiElement && ((PsiElement) element).isValid()) {
PsiElement psiElement = getElement(((PsiElement) element), popup);
psiElement = psiElement.getNavigationElement();
VirtualFile file = PsiUtilCore.getVirtualFile(psiElement);
if (file != null && popup.getLinePosition() != -1) {
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, popup.getLinePosition(), popup.getColumnPosition());
Navigatable n = descriptor.setUseCurrentWindow(popup.isOpenInCurrentWindowRequested());
if (n.canNavigate()) {
n.navigate(true);
return;
}
}
if (file != null && popup.getMemberPattern() != null) {
NavigationUtil.activateFileWithPsiElement(psiElement, !popup.isOpenInCurrentWindowRequested());
Navigatable member = findMember(popup.getMemberPattern(), psiElement, file);
if (member != null) {
member.navigate(true);
}
}
NavigationUtil.activateFileWithPsiElement(psiElement, !popup.isOpenInCurrentWindowRequested());
} else {
EditSourceUtil.navigate(((NavigationItem) element), true, popup.isOpenInCurrentWindowRequested());
}
});
}
}, IdeBundle.message("go.to.class.toolwindow.title"), true);
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class LRUPopupBuilder method changeLanguageWithUndo.
private static void changeLanguageWithUndo(@NotNull Project project, @NotNull final Language t, @NotNull final VirtualFile[] sortedFiles, @NotNull final PerFileMappings<Language> mappings) throws UnexpectedUndoException {
ReadonlyStatusHandler.OperationStatus status = ReadonlyStatusHandler.getInstance(project).ensureFilesWritable(sortedFiles);
if (status.hasReadonlyFiles())
return;
final Set<VirtualFile> matchedExtensions = ContainerUtil.newLinkedHashSet();
final Map<VirtualFile, Language> oldMapping = ContainerUtil.newHashMap();
for (VirtualFile file : sortedFiles) {
oldMapping.put(file, mappings.getMapping(file));
if (ScratchUtil.hasMatchingExtension(project, file)) {
matchedExtensions.add(file);
}
}
BasicUndoableAction action = new BasicUndoableAction(sortedFiles) {
@Override
public void undo() throws UnexpectedUndoException {
for (VirtualFile file : sortedFiles) {
mappings.setMapping(file, oldMapping.get(file));
}
}
@Override
public void redo() throws UnexpectedUndoException {
for (VirtualFile file : sortedFiles) {
mappings.setMapping(file, t);
}
}
};
action.redo();
UndoManager.getInstance(project).undoableActionPerformed(action);
for (VirtualFile file : matchedExtensions) {
try {
ScratchUtil.updateFileExtension(project, file);
} catch (IOException ignored) {
}
}
}
use of com.intellij.lang.Language in project intellij-community by JetBrains.
the class RootType method substituteIcon.
@Nullable
public Icon substituteIcon(@NotNull Project project, @NotNull VirtualFile file) {
Language language = substituteLanguage(project, file);
FileType fileType = LanguageUtil.getLanguageFileType(language);
if (fileType == null)
fileType = ScratchUtil.getFileTypeFromName(file);
return fileType != null ? fileType.getIcon() : null;
}
Aggregations