Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocompletion of imports, class names, and static fields and methods after a classname #47

Merged
merged 16 commits into from
Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/org/scijava/ui/swing/script/EditorPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.scijava.script.ScriptHeaderService;
import org.scijava.script.ScriptLanguage;
import org.scijava.script.ScriptService;
import org.scijava.ui.swing.script.autocompletion.JythonAutoCompletion;
import org.scijava.util.FileUtils;

/**
Expand Down Expand Up @@ -106,6 +107,8 @@ public class EditorPane extends RSyntaxTextArea implements DocumentListener {
private PrefService prefService;
@Parameter
private LogService log;

private JythonAutoCompletion autoCompletionProxy;

/**
* Constructor.
Expand Down
96 changes: 94 additions & 2 deletions src/main/java/org/scijava/ui/swing/script/TextEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package org.scijava.ui.swing.script;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
Expand Down Expand Up @@ -108,6 +109,7 @@
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
Expand Down Expand Up @@ -156,6 +158,7 @@
import org.scijava.thread.ThreadService;
import org.scijava.ui.CloseConfirmable;
import org.scijava.ui.UIService;
import org.scijava.ui.swing.script.autocompletion.ClassUtil;
import org.scijava.ui.swing.script.commands.ChooseFontSize;
import org.scijava.ui.swing.script.commands.ChooseTabSize;
import org.scijava.ui.swing.script.commands.GitGrep;
Expand Down Expand Up @@ -214,7 +217,7 @@ public class TextEditor extends JFrame implements ActionListener,
openMacroFunctions, decreaseFontSize, increaseFontSize, chooseFontSize,
chooseTabSize, gitGrep, openInGitweb, replaceTabsWithSpaces,
replaceSpacesWithTabs, toggleWhiteSpaceLabeling, zapGremlins,
savePreferences, toggleAutoCompletionMenu;
savePreferences, toggleAutoCompletionMenu, openClassOrPackageHelp;
private RecentFilesMenuItem openRecent;
private JMenu gitMenu, tabsMenu, fontSizeMenu, tabSizeMenu, toolsMenu,
runMenu, whiteSpaceMenu;
Expand Down Expand Up @@ -483,6 +486,8 @@ public TextEditor(final Context context) {
openHelp =
addToMenu(toolsMenu, "Open Help for Class (with frames)...", 0, 0);
openHelp.setMnemonic(KeyEvent.VK_P);
openClassOrPackageHelp = addToMenu(toolsMenu, "Source or javadoc for class or package...", 0, 0);
openClassOrPackageHelp.setMnemonic(KeyEvent.VK_S);
openMacroFunctions =
addToMenu(toolsMenu, "Open Help on Macro Functions...", 0, 0);
openMacroFunctions.setMnemonic(KeyEvent.VK_H);
Expand Down Expand Up @@ -988,7 +993,12 @@ public void loadPreferences() {

final int windowWidth = prefService.getInt(getClass(), WINDOW_WIDTH, dim.width);
final int windowHeight = prefService.getInt(getClass(), WINDOW_HEIGHT, dim.height);
setPreferredSize(new Dimension(windowWidth, windowHeight));
// Avoid creating a window larger than the desktop
final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
if (windowWidth > screen.getWidth() || windowHeight > screen.getHeight())
setPreferredSize(new Dimension(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT));
else
setPreferredSize(new Dimension(windowWidth, windowHeight));

final int mainDivLocation = prefService.getInt(getClass(), MAIN_DIV_LOCATION, body.getDividerLocation());
body.setDividerLocation(mainDivLocation);
Expand Down Expand Up @@ -1395,6 +1405,7 @@ else if (source == savePreferences) {
}
else if (source == openHelp) openHelp(null);
else if (source == openHelpWithoutFrames) openHelp(null, false);
else if (source == openClassOrPackageHelp) openClassOrPackageHelp(null);
else if (source == openMacroFunctions) try {
new MacroFunctions(this).openHelp(getTextArea().getSelectedText());
}
Expand Down Expand Up @@ -2659,6 +2670,87 @@ public void openHelp(String className, final boolean withFrames) {
handleException(e);
}
}

/**
* @param text Either a classname, or a partial class name, or package name or any part of the fully qualified class name.
*/
public void openClassOrPackageHelp(String text) {
if (text == null)
text = getSelectedClassNameOrAsk();
if (null == text) return;
new Thread(new FindClassSourceAndJavadoc(text)).start(); // fork away from event dispatch thread
}

public class FindClassSourceAndJavadoc implements Runnable {
private final String text;
public FindClassSourceAndJavadoc(final String text) {
this.text = text;
}
@Override
public void run() {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
final HashMap<String, ArrayList<String>> matches;
try {
matches = ClassUtil.findDocumentationForClass(text);
} finally {
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
if (matches.isEmpty()) {
JOptionPane.showMessageDialog(getEditorPane(), "No info found for:\n'" + text +'"');
return;
}
final JPanel panel = new JPanel();
final GridBagLayout gridbag = new GridBagLayout();
final GridBagConstraints c = new GridBagConstraints();
panel.setLayout(gridbag);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
final List<String> keys = new ArrayList<String>(matches.keySet());
Collections.sort(keys);
c.gridy = 0;
for (final String classname: keys) {
c.gridx = 0;
c.anchor = GridBagConstraints.EAST;
final JLabel class_label = new JLabel(classname);
gridbag.setConstraints(class_label, c);
panel.add(class_label);
ArrayList<String> urls = matches.get(classname);
if (urls.isEmpty()) {
urls = new ArrayList<String>();
urls.add("https://duckduckgo.com/?q=" + classname);
}
for (final String url: urls) {
c.gridx += 1;
c.anchor = GridBagConstraints.WEST;
String title = "JavaDoc";
if (url.endsWith(".java")) title = "Source";
else if (url.contains("duckduckgo")) title = "Search...";
final JButton link = new JButton(title);
gridbag.setConstraints(link, c);
panel.add(link);
link.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent event) {
try {
platformService.open(new URL(url));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
c.gridy += 1;
}
final JScrollPane jsp = new JScrollPane(panel);
//jsp.setPreferredSize(new Dimension(800, 500));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame(text);
frame.getContentPane().add(jsp);
frame.pack();
frame.setVisible(true);
}
});
}
}

public void extractSourceJar() {
final File file = openWithDialog(null);
Expand Down
Loading