mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
CTRL+f12 in selected editor (diff views)
This commit is contained in:
@@ -15,12 +15,11 @@
|
||||
*/
|
||||
package com.intellij.openapi.fileEditor;
|
||||
|
||||
import com.intellij.ide.*;
|
||||
import com.intellij.ide.FileEditorProvider;
|
||||
import com.intellij.ide.SelectInContext;
|
||||
import com.intellij.ide.SelectInManager;
|
||||
import com.intellij.ide.SelectInTarget;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.RangeMarker;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.actionSystem.DataKey;
|
||||
import com.intellij.openapi.editor.*;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.fileTypes.FileTypeManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -32,9 +31,12 @@ import com.intellij.psi.PsiManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.List;
|
||||
|
||||
public class OpenFileDescriptor implements Navigatable {
|
||||
public static final DataKey<Editor> NAVIGATE_IN_EDITOR = DataKey.create("NAVIGATE_IN_EDITOR");
|
||||
|
||||
@NotNull private final VirtualFile myFile;
|
||||
private final int myOffset;
|
||||
private final int myLine;
|
||||
@@ -95,54 +97,100 @@ public class OpenFileDescriptor implements Navigatable {
|
||||
}
|
||||
|
||||
public void navigate(boolean requestFocus) {
|
||||
if (myProject == null) {
|
||||
if (!canNavigate()) {
|
||||
throw new IllegalStateException("Navigation is not possible with null project");
|
||||
}
|
||||
|
||||
if (navigateDirectory(requestFocus)) return;
|
||||
if (navigateInEditor(myProject, requestFocus)) return;
|
||||
|
||||
navigateInProjectView();
|
||||
}
|
||||
|
||||
private boolean navigateDirectory(final boolean requestFocus) {
|
||||
if (myFile != null && myFile.isDirectory()) {
|
||||
final PsiDirectory directory = PsiManager.getInstance(myProject).findDirectory(myFile);
|
||||
if (directory != null) {
|
||||
directory.navigate(requestFocus);
|
||||
return;
|
||||
}
|
||||
}
|
||||
FileEditor fileEditor = openFileAskingType(myProject, requestFocus);
|
||||
if (fileEditor == null) {
|
||||
final SelectInTarget projectSelector = SelectInManager.getInstance(myProject).getTarget(SelectInManager.PROJECT);
|
||||
if (projectSelector != null) {
|
||||
projectSelector.selectIn(new SelectInContext() {
|
||||
@NotNull
|
||||
public Project getProject() {
|
||||
return myProject;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public VirtualFile getVirtualFile() {
|
||||
return myFile;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Object getSelectorInFile() {
|
||||
return myFile.isValid() ? PsiManager.getInstance(myProject).findFile(myFile) : null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public FileEditorProvider getFileEditorProvider() {
|
||||
return null;
|
||||
}
|
||||
}, true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private FileEditor openFileAskingType(Project project, boolean focusEditor) {
|
||||
private boolean navigateInEditor(Project project, boolean focusEditor) {
|
||||
FileType type = FileTypeManager.getInstance().getKnownFileTypeOrAssociate(myFile);
|
||||
if (type == null || myFile == null || !myFile.isValid()) return null;
|
||||
if (type == null || myFile == null || !myFile.isValid()) return false;
|
||||
|
||||
final List<FileEditor> fileEditors = FileEditorManager.getInstance(project).openEditor(this, focusEditor);
|
||||
if (fileEditors.isEmpty()) return null;
|
||||
return fileEditors.get(0);
|
||||
if (navigateInRequestedEditor()) return true;
|
||||
if (navigateInAnyFileEditor(project, focusEditor)) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean navigateInRequestedEditor() {
|
||||
DataContext ctx = DataManager.getInstance().getDataContext();
|
||||
Editor e = NAVIGATE_IN_EDITOR.getData(ctx);
|
||||
if (e == null) return false;
|
||||
|
||||
navigateIn(e);
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean navigateInAnyFileEditor(Project project, boolean focusEditor) {
|
||||
List<FileEditor> editors = FileEditorManager.getInstance(project).openEditor(this, focusEditor);
|
||||
return !editors.isEmpty();
|
||||
}
|
||||
|
||||
private void navigateInProjectView() {
|
||||
SelectInTarget selector = SelectInManager.getInstance(myProject).getTarget(SelectInManager.PROJECT);
|
||||
if (selector == null) return;
|
||||
|
||||
selector.selectIn(new SelectInContext() {
|
||||
@NotNull
|
||||
public Project getProject() {
|
||||
return myProject;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public VirtualFile getVirtualFile() {
|
||||
return myFile;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Object getSelectorInFile() {
|
||||
return myFile.isValid() ? PsiManager.getInstance(myProject).findFile(myFile) : null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public FileEditorProvider getFileEditorProvider() {
|
||||
return null;
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
public void navigateIn(Editor e) {
|
||||
if (getOffset() >= 0) {
|
||||
e.getCaretModel().moveToOffset(Math.min(getOffset(), e.getDocument().getTextLength()));
|
||||
}
|
||||
else if (getLine() != -1 && getColumn() != -1) {
|
||||
LogicalPosition pos = new LogicalPosition(getLine(), getColumn());
|
||||
e.getCaretModel().moveToLogicalPosition(pos);
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
|
||||
e.getSelectionModel().removeSelection();
|
||||
scrollToCaret(e);
|
||||
}
|
||||
|
||||
private void scrollToCaret(final Editor e) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
e.getScrollingModel().scrollToCaret(ScrollType.CENTER);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public boolean canNavigate() {
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
|
||||
package com.intellij.ide.util;
|
||||
|
||||
import com.intellij.ide.IdeBundle;
|
||||
import com.intellij.ide.commander.CommanderPanel;
|
||||
import com.intellij.ide.commander.ProjectListBuilder;
|
||||
import com.intellij.ide.structureView.StructureViewBuilder;
|
||||
import com.intellij.ide.structureView.StructureViewModel;
|
||||
import com.intellij.ide.structureView.StructureViewTreeElement;
|
||||
import com.intellij.ide.structureView.TreeBasedStructureViewBuilder;
|
||||
import com.intellij.ide.structureView.StructureViewBuilder;
|
||||
import com.intellij.ide.util.treeView.AbstractTreeNode;
|
||||
import com.intellij.ide.util.treeView.AbstractTreeStructure;
|
||||
import com.intellij.ide.util.treeView.smartTree.SmartTreeStructure;
|
||||
@@ -16,6 +15,7 @@ import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.actionSystem.*;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
@@ -52,7 +52,10 @@ public class FileStructureDialog extends DialogWrapper {
|
||||
@NonNls private static final String ourPropertyKey = "FileStructure.narrowDown";
|
||||
private boolean myShouldNarrowDown = false;
|
||||
|
||||
public FileStructureDialog(StructureViewModel structureViewModel, @Nullable Editor editor, Project project, Navigatable navigatable,
|
||||
public FileStructureDialog(StructureViewModel structureViewModel,
|
||||
@Nullable Editor editor,
|
||||
Project project,
|
||||
Navigatable navigatable,
|
||||
@NotNull final Disposable auxDisposable) {
|
||||
super(project, true);
|
||||
myProject = project;
|
||||
@@ -84,7 +87,7 @@ public class FileStructureDialog extends DialogWrapper {
|
||||
return PsiDocumentManager.getInstance(project).getPsiFile(myEditor.getDocument());
|
||||
}
|
||||
|
||||
protected Border createContentPaneBorder(){
|
||||
protected Border createContentPaneBorder() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -93,7 +96,7 @@ public class FileStructureDialog extends DialogWrapper {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
protected String getDimensionServiceKey(){
|
||||
protected String getDimensionServiceKey() {
|
||||
return "#com.intellij.ide.util.FileStructureDialog";
|
||||
}
|
||||
|
||||
@@ -136,10 +139,10 @@ public class FileStructureDialog extends DialogWrapper {
|
||||
return psiClass;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Object elementAtCursor = myTreeModel.getCurrentEditorElement();
|
||||
if (elementAtCursor instanceof PsiElement) {
|
||||
return (PsiElement) elementAtCursor;
|
||||
return (PsiElement)elementAtCursor;
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -152,7 +155,8 @@ public class FileStructureDialog extends DialogWrapper {
|
||||
|
||||
PsiFile psiFile = getPsiFile(myProject);
|
||||
StructureViewBuilder viewBuilder = psiFile.getLanguage().getStructureViewBuilder(psiFile);
|
||||
boolean showRoot = viewBuilder instanceof TreeBasedStructureViewBuilder && ((TreeBasedStructureViewBuilder)viewBuilder).isRootNodeShown();
|
||||
boolean showRoot =
|
||||
viewBuilder instanceof TreeBasedStructureViewBuilder && ((TreeBasedStructureViewBuilder)viewBuilder).isRootNodeShown();
|
||||
ProjectListBuilder projectListBuilder = new ProjectListBuilder(myProject, myCommanderPanel, treeStructure, null, showRoot) {
|
||||
protected boolean nodeIsAcceptableForElement(AbstractTreeNode node, Object element) {
|
||||
return Comparing.equal(((StructureViewTreeElement)node.getValue()).getValue(), element);
|
||||
@@ -184,13 +188,13 @@ public class FileStructureDialog extends DialogWrapper {
|
||||
}
|
||||
}.registerCustomShortcutSet(ActionManager.getInstance().getAction(IdeActions.ACTION_EDIT_SOURCE).getShortcutSet(), myCommanderPanel);
|
||||
|
||||
myCommanderPanel.setPreferredSize(new Dimension(400,500));
|
||||
myCommanderPanel.setPreferredSize(new Dimension(400, 500));
|
||||
|
||||
JPanel panel = new JPanel(new GridBagLayout());
|
||||
final JCheckBox checkBox = new JCheckBox(IdeBundle.message("checkbox.narrow.down.the.list.on.typing"));
|
||||
checkBox.setSelected(PropertiesComponent.getInstance().isTrueValue(ourPropertyKey));
|
||||
checkBox.addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent e){
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
myShouldNarrowDown = checkBox.isSelected();
|
||||
PropertiesComponent.getInstance().setValue(ourPropertyKey, Boolean.toString(myShouldNarrowDown));
|
||||
|
||||
@@ -204,8 +208,10 @@ public class FileStructureDialog extends DialogWrapper {
|
||||
|
||||
checkBox.setFocusable(false);
|
||||
|
||||
panel.add(checkBox, new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(0,5,0,5),0,0));
|
||||
panel.add(myCommanderPanel, new GridBagConstraints(0,1,1,1,1,1,GridBagConstraints.WEST,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
|
||||
panel.add(checkBox,
|
||||
new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
|
||||
panel.add(myCommanderPanel,
|
||||
new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
|
||||
|
||||
return panel;
|
||||
}
|
||||
@@ -227,24 +233,24 @@ public class FileStructureDialog extends DialogWrapper {
|
||||
super(_project, false);
|
||||
myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
myListSpeedSearch.addChangeListener(new PropertyChangeListener() {
|
||||
public void propertyChange(PropertyChangeEvent evt){
|
||||
public void propertyChange(PropertyChangeEvent evt) {
|
||||
ProjectListBuilder builder = (ProjectListBuilder)getBuilder();
|
||||
if (builder == null) {
|
||||
return;
|
||||
}
|
||||
builder.addUpdateRequest(hasPrefixShortened(evt));
|
||||
ApplicationManager.getApplication().invokeLater(new Runnable() {
|
||||
public void run(){
|
||||
int index = myList.getSelectedIndex();
|
||||
if (index != -1 && index < myList.getModel().getSize()) {
|
||||
myList.clearSelection();
|
||||
ListScrollingUtil.selectItem(myList, index);
|
||||
}
|
||||
else {
|
||||
ListScrollingUtil.ensureSelectionExists(myList);
|
||||
}
|
||||
}
|
||||
});
|
||||
public void run() {
|
||||
int index = myList.getSelectedIndex();
|
||||
if (index != -1 && index < myList.getModel().getSize()) {
|
||||
myList.clearSelection();
|
||||
ListScrollingUtil.selectItem(myList, index);
|
||||
}
|
||||
else {
|
||||
ListScrollingUtil.ensureSelectionExists(myList);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
myListSpeedSearch.setComparator(createSpeedSearchComparator());
|
||||
@@ -256,6 +262,7 @@ public class FileStructureDialog extends DialogWrapper {
|
||||
}
|
||||
|
||||
public boolean navigateSelectedElement() {
|
||||
|
||||
final boolean succeeded = super.navigateSelectedElement();
|
||||
if (succeeded) {
|
||||
close(CANCEL_EXIT_CODE);
|
||||
@@ -271,10 +278,13 @@ public class FileStructureDialog extends DialogWrapper {
|
||||
if (DataConstants.NAVIGATABLE.equals(dataId)) {
|
||||
return selectedElement instanceof Navigatable ? selectedElement : myNavigatable;
|
||||
}
|
||||
|
||||
if (OpenFileDescriptor.NAVIGATE_IN_EDITOR.getName().equals(dataId)) return myEditor;
|
||||
|
||||
return getDataImpl(dataId);
|
||||
}
|
||||
|
||||
public String getEnteredPrefix(){
|
||||
public String getEnteredPrefix() {
|
||||
return myListSpeedSearch.getEnteredPrefix();
|
||||
}
|
||||
|
||||
@@ -288,7 +298,7 @@ public class FileStructureDialog extends DialogWrapper {
|
||||
super(FileStructureDialog.this.myProject, myTreeModel);
|
||||
}
|
||||
|
||||
public Object[] getChildElements(Object element){
|
||||
public Object[] getChildElements(Object element) {
|
||||
Object[] childElements = super.getChildElements(element);
|
||||
|
||||
if (!myShouldNarrowDown) {
|
||||
|
||||
@@ -191,30 +191,7 @@ public final class TextEditorImpl extends UserDataHolderBase implements TextEdit
|
||||
}
|
||||
|
||||
public void navigateTo(@NotNull final Navigatable navigatable) {
|
||||
assert navigatable instanceof OpenFileDescriptor;
|
||||
|
||||
final OpenFileDescriptor descriptor = (OpenFileDescriptor)navigatable;
|
||||
|
||||
final Editor _editor = getEditor();
|
||||
|
||||
if (descriptor.getOffset() >= 0) {
|
||||
_editor.getCaretModel().moveToOffset(Math.min(descriptor.getOffset(), _editor.getDocument().getTextLength()));
|
||||
_editor.getSelectionModel().removeSelection();
|
||||
scrollToCaret(_editor);
|
||||
}
|
||||
else if (descriptor.getLine() != -1 && descriptor.getColumn() != -1) {
|
||||
final LogicalPosition pos = new LogicalPosition(descriptor.getLine(), descriptor.getColumn());
|
||||
_editor.getCaretModel().moveToLogicalPosition(pos);
|
||||
_editor.getSelectionModel().removeSelection();
|
||||
scrollToCaret(_editor);
|
||||
}
|
||||
}
|
||||
|
||||
private static void scrollToCaret(final Editor _editor) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
_editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
|
||||
}
|
||||
});
|
||||
OpenFileDescriptor d = (OpenFileDescriptor)navigatable;
|
||||
d.navigateIn(getEditor());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user