eppleton commented on code in PR #9812:
URL: https://github.com/apache/netbeans/pull/9812#discussion_r4628674201


##########
platform/core.output2/src/org/netbeans/core/output2/ui/AbstractOutputPane.java:
##########
@@ -1025,4 +1037,237 @@ public void setSelectionVisible(boolean vis) {
             }
         }
     }
+    
+    
+    private static class HoverSupport {
+        private final JEditorPane pane;
+
+        private Object hightlightHandle;
+        private int[] highlightRange;
+        private Point lastMouse;            // so key presses can re-evaluate 
without mouse movement
+
+        public HoverSupport(JEditorPane pane) {
+            this.pane = pane;
+
+            MouseAdapter mouse = new MouseAdapter() {
+                @Override
+                public void mouseMoved(MouseEvent e) {
+                    lastMouse = e.getPoint();
+                    update(isCtrlDown(e));
+                }
+
+                @Override
+                public void mouseDragged(MouseEvent e) {
+                    clear();
+                }
+
+                @Override
+                public void mouseExited(MouseEvent e) {
+                    lastMouse = null;
+                    clear();
+                }
+
+                @Override
+                public void mouseClicked(MouseEvent e) {
+                    if (isCtrlDown(e) && highlightRange != null) {
+                        onActivate(highlightRange[0], highlightRange[1]);
+                        e.consume();
+                    }
+                }
+            };
+            pane.addMouseListener(mouse);
+            pane.addMouseMotionListener(mouse);
+
+            pane.addKeyListener(new KeyAdapter() {
+                @Override
+                public void keyPressed(KeyEvent e) {
+                    update(isCtrlDown(e));
+                }
+
+                @Override
+                public void keyReleased(KeyEvent e) {
+                    update(isCtrlDown(e));
+                }
+            });
+        }
+
+        private static boolean isCtrlDown(InputEvent e) {
+            // Cmd on Mac OS X, Ctrl elsewhere
+            int MOD = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();
+            return (e.getModifiersEx() & MOD) != 0;
+        }
+
+        private void update(boolean modifierDown) {
+            if (!modifierDown || lastMouse == null) {
+                clear();
+                return;
+            }
+
+            int offset = offsetAt(lastMouse);
+            if (offset < 0) {
+                clear();
+                return;
+            }
+
+            try {
+                int[] startEnd = findPathBoundaries(pane, offset);
+                String path = startEnd == null ? "" : 
pane.getDocument().getText(startEnd[0], startEnd[1] - startEnd[0]);
+                if (path.isBlank()) {
+                    clear();
+                    return;
+                }
+                if (highlightRange != null && startEnd[0] == highlightRange[0] 
&& startEnd[1] == highlightRange[1]) {
+                    return;   // unchanged, avoid repaint churn
+                }
+                clear();
+                hightlightHandle = 
pane.getHighlighter().addHighlight(startEnd[0], startEnd[1], UNDERLINE);
+                highlightRange = startEnd;
+                pane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
+            } catch (BadLocationException ex) {
+                clear();
+            }
+        }
+
+        /**
+         * Document offset under the point, or -1 if the pointer isn't actually
+         * over a character.
+         */
+        private int offsetAt(Point p) {
+            Position.Bias[] bias = new Position.Bias[1];
+            int offset = pane.getUI().viewToModel2D(pane, p, bias);
+            if (offset < 0) {
+                return -1;
+            }
+            // viewToModel returns the *nearest* offset, even in empty space 
past line end — verify
+            if (bias[0] == Position.Bias.Backward && offset > 0) {
+                offset--;
+            }
+            try {
+                int len = pane.getDocument().getLength();
+                if (offset >= len) {
+                    return -1;
+                }
+                Rectangle2D a = pane.modelToView2D(offset);
+                Rectangle2D b = pane.modelToView2D(Math.min(offset + 1, len));
+                double right = (b.getY() == a.getY()) ? b.getX() : a.getX() + 
8; // char box
+                return (p.x >= a.getX() && p.x <= right
+                        && p.y >= a.getY() && p.y <= a.getY() + a.getHeight()) 
? offset : -1;
+            } catch (BadLocationException e) {
+                return -1;
+            }
+        }
+
+        private void clear() {
+            if (hightlightHandle != null) {
+                pane.getHighlighter().removeHighlight(hightlightHandle);
+                hightlightHandle = null;
+            }
+            highlightRange = null;
+            pane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
+        }
+
+        private void onActivate(int start, int end) {
+            try {
+                String path = pane.getText(start, end - start);
+                File file = new File(path);
+                if (file.exists()) {
+                    if (FileUtil.toFileObject(file) instanceof FileObject 
toOpen) {
+                        if (toOpen.getLookup().lookup(Openable.class) 
instanceof Openable doIt) {
+                            doIt.open();
+                            return;
+                        }
+                    }
+                }
+            } catch (BadLocationException ex) {
+                ex.printStackTrace();
+            }
+            Toolkit.getDefaultToolkit().beep();
+        }
+
+        /**
+         * Painter that draws an underline instead of filling the background.
+         */
+        private static final Highlighter.HighlightPainter UNDERLINE
+                = new LayeredHighlighter.LayerPainter() {
+            @Override
+            public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds,
+                    JTextComponent c, View view) {
+                try {
+                    Shape s = view.modelToView(p0, Position.Bias.Forward,
+                            p1, Position.Bias.Backward, bounds);
+                    Rectangle r = s.getBounds();
+                    g.setColor(new Color(0x2060D0));
+                    int y = r.y + r.height - 2;
+                    g.drawLine(r.x, y, r.x + r.width - 1, y);
+                    return r;
+                } catch (BadLocationException e) {
+                    return null;
+                }
+            }
+
+            @Override
+            public void paint(Graphics g, int p0, int p1, Shape bounds, 
JTextComponent c) {
+                // not used: DefaultHighlighter calls paintLayer for 
LayerPainters
+            }
+        };
+
+        private static int[] findPathBoundaries(JEditorPane pane, int offset) 
throws BadLocationException {
+            int start = Utilities.getWordStart(pane, offset);
+            int end = Utilities.getWordEnd(pane, offset);
+            
+            Set<String> candidates = new TreeSet<>((s1, s2) -> {
+                int lenDiff = s2.length() - s1.length();
+                if (lenDiff != 0) {
+                    return lenDiff;
+                } else {
+                    return s2.compareTo(s1);
+                }
+            });
+            while (start < end) {
+                candidates.add(pane.getText(start, end - start));
+                start--;
+                if (start < 0 || !isPathSeparator(pane.getText(start, 1))) {
+                    break;
+                } else {
+                    candidates.add(pane.getText(start, end - start));
+                    start = Utilities.getPreviousWord(pane, start);
+                }
+            }
+            
+            File existingPrefix = null;
+            for (String path : candidates) {
+                File f = new File(path).getAbsoluteFile();
+                if (f.exists()) {
+                    existingPrefix = f;
+                    start = end - path.length();
+                    break;
+                }
+            }
+            if (existingPrefix == null) {
+                return null;
+            }
+            while (end + 1 < pane.getDocument().getLength()) {
+                if (!isPathSeparator(pane.getText(end, 1))) {
+                    break;
+                }
+                int nextEnd = Utilities.getNextWord(pane, end + 1);
+                if (nextEnd <= end) {
+                    break;
+                }
+                String nextChild = pane.getText(end + 1, nextEnd - end - 1);
+                File nextFile = new File(existingPrefix, nextChild);
+                if (nextFile.exists()) {
+                    existingPrefix = nextFile;
+                    end = nextEnd;
+                } else {
+                    break;
+                }
+            }
+            return new int[] { start, end };
+        }
+        
+        private static boolean isPathSeparator(String str) {
+            return "/".equals(str) || File.pathSeparator.equals(str);

Review Comment:
   `File.pathSeparator` is the classpath separator (`:` on Unix, `;` on 
Windows), not the file separator. On Windows, backslash paths are never joined, 
so only the single word under the mouse is checked and absolute Windows paths 
won't be found. On Unix, `:` is treated as a separator, which only works by 
accident for `Foo.java:12`. I think `File.separator` (or both `/` and `\` on 
Windows) was meant.



##########
platform/core.output2/src/org/netbeans/core/output2/ui/AbstractOutputPane.java:
##########
@@ -1025,4 +1037,237 @@ public void setSelectionVisible(boolean vis) {
             }
         }
     }
+    
+    
+    private static class HoverSupport {
+        private final JEditorPane pane;
+
+        private Object hightlightHandle;
+        private int[] highlightRange;
+        private Point lastMouse;            // so key presses can re-evaluate 
without mouse movement
+
+        public HoverSupport(JEditorPane pane) {
+            this.pane = pane;
+
+            MouseAdapter mouse = new MouseAdapter() {
+                @Override
+                public void mouseMoved(MouseEvent e) {
+                    lastMouse = e.getPoint();
+                    update(isCtrlDown(e));
+                }
+
+                @Override
+                public void mouseDragged(MouseEvent e) {
+                    clear();
+                }
+
+                @Override
+                public void mouseExited(MouseEvent e) {
+                    lastMouse = null;
+                    clear();
+                }
+
+                @Override
+                public void mouseClicked(MouseEvent e) {
+                    if (isCtrlDown(e) && highlightRange != null) {
+                        onActivate(highlightRange[0], highlightRange[1]);
+                        e.consume();
+                    }
+                }
+            };
+            pane.addMouseListener(mouse);
+            pane.addMouseMotionListener(mouse);
+
+            pane.addKeyListener(new KeyAdapter() {
+                @Override
+                public void keyPressed(KeyEvent e) {
+                    update(isCtrlDown(e));
+                }
+
+                @Override
+                public void keyReleased(KeyEvent e) {
+                    update(isCtrlDown(e));
+                }
+            });
+        }
+
+        private static boolean isCtrlDown(InputEvent e) {
+            // Cmd on Mac OS X, Ctrl elsewhere
+            int MOD = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();
+            return (e.getModifiersEx() & MOD) != 0;
+        }
+
+        private void update(boolean modifierDown) {
+            if (!modifierDown || lastMouse == null) {
+                clear();
+                return;
+            }
+
+            int offset = offsetAt(lastMouse);
+            if (offset < 0) {
+                clear();
+                return;
+            }
+
+            try {
+                int[] startEnd = findPathBoundaries(pane, offset);
+                String path = startEnd == null ? "" : 
pane.getDocument().getText(startEnd[0], startEnd[1] - startEnd[0]);
+                if (path.isBlank()) {
+                    clear();
+                    return;
+                }
+                if (highlightRange != null && startEnd[0] == highlightRange[0] 
&& startEnd[1] == highlightRange[1]) {
+                    return;   // unchanged, avoid repaint churn
+                }
+                clear();
+                hightlightHandle = 
pane.getHighlighter().addHighlight(startEnd[0], startEnd[1], UNDERLINE);
+                highlightRange = startEnd;
+                pane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
+            } catch (BadLocationException ex) {
+                clear();
+            }
+        }
+
+        /**
+         * Document offset under the point, or -1 if the pointer isn't actually
+         * over a character.
+         */
+        private int offsetAt(Point p) {
+            Position.Bias[] bias = new Position.Bias[1];
+            int offset = pane.getUI().viewToModel2D(pane, p, bias);
+            if (offset < 0) {
+                return -1;
+            }
+            // viewToModel returns the *nearest* offset, even in empty space 
past line end — verify
+            if (bias[0] == Position.Bias.Backward && offset > 0) {
+                offset--;
+            }
+            try {
+                int len = pane.getDocument().getLength();
+                if (offset >= len) {
+                    return -1;
+                }
+                Rectangle2D a = pane.modelToView2D(offset);
+                Rectangle2D b = pane.modelToView2D(Math.min(offset + 1, len));
+                double right = (b.getY() == a.getY()) ? b.getX() : a.getX() + 
8; // char box
+                return (p.x >= a.getX() && p.x <= right
+                        && p.y >= a.getY() && p.y <= a.getY() + a.getHeight()) 
? offset : -1;
+            } catch (BadLocationException e) {
+                return -1;
+            }
+        }
+
+        private void clear() {
+            if (hightlightHandle != null) {
+                pane.getHighlighter().removeHighlight(hightlightHandle);
+                hightlightHandle = null;
+            }
+            highlightRange = null;
+            pane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));

Review Comment:
   `clear()` always sets `TEXT_CURSOR`. `AbstractOutputPane.mouseMoved` sets 
the hand cursor over existing hyperlinks, but `HoverSupport` registers its 
listener after that one, and on every mouse move without Ctrl it calls 
`clear()`, which resets the cursor to text. From reading the code, existing 
hyperlinks like compiler errors and stack traces would lose their hand cursor. 
Only resetting the cursor when this class changed it would avoid that.



##########
platform/core.output2/src/org/netbeans/core/output2/ui/AbstractOutputPane.java:
##########
@@ -1025,4 +1037,237 @@ public void setSelectionVisible(boolean vis) {
             }
         }
     }
+    
+    
+    private static class HoverSupport {
+        private final JEditorPane pane;
+
+        private Object hightlightHandle;
+        private int[] highlightRange;
+        private Point lastMouse;            // so key presses can re-evaluate 
without mouse movement
+
+        public HoverSupport(JEditorPane pane) {
+            this.pane = pane;
+
+            MouseAdapter mouse = new MouseAdapter() {
+                @Override
+                public void mouseMoved(MouseEvent e) {
+                    lastMouse = e.getPoint();
+                    update(isCtrlDown(e));
+                }
+
+                @Override
+                public void mouseDragged(MouseEvent e) {
+                    clear();
+                }
+
+                @Override
+                public void mouseExited(MouseEvent e) {
+                    lastMouse = null;
+                    clear();
+                }
+
+                @Override
+                public void mouseClicked(MouseEvent e) {
+                    if (isCtrlDown(e) && highlightRange != null) {
+                        onActivate(highlightRange[0], highlightRange[1]);

Review Comment:
   `mouseReleased` calls `lineClicked` no matter which modifiers are held. On a 
line that is already a hyperlink and also contains a path (for example a Maven 
`/abs/path/Foo.java:[12,5]` error), Ctrl+click would probably run both the 
hyperlink action and `Openable.open()`. I haven't verified what the user ends 
up seeing.



##########
platform/core.output2/src/org/netbeans/core/output2/ui/AbstractOutputPane.java:
##########
@@ -1025,4 +1037,237 @@ public void setSelectionVisible(boolean vis) {
             }
         }
     }
+    
+    
+    private static class HoverSupport {
+        private final JEditorPane pane;
+
+        private Object hightlightHandle;
+        private int[] highlightRange;
+        private Point lastMouse;            // so key presses can re-evaluate 
without mouse movement
+
+        public HoverSupport(JEditorPane pane) {
+            this.pane = pane;
+
+            MouseAdapter mouse = new MouseAdapter() {
+                @Override
+                public void mouseMoved(MouseEvent e) {
+                    lastMouse = e.getPoint();
+                    update(isCtrlDown(e));
+                }
+
+                @Override
+                public void mouseDragged(MouseEvent e) {
+                    clear();
+                }
+
+                @Override
+                public void mouseExited(MouseEvent e) {
+                    lastMouse = null;
+                    clear();
+                }
+
+                @Override
+                public void mouseClicked(MouseEvent e) {
+                    if (isCtrlDown(e) && highlightRange != null) {
+                        onActivate(highlightRange[0], highlightRange[1]);
+                        e.consume();
+                    }
+                }
+            };
+            pane.addMouseListener(mouse);
+            pane.addMouseMotionListener(mouse);
+
+            pane.addKeyListener(new KeyAdapter() {
+                @Override
+                public void keyPressed(KeyEvent e) {
+                    update(isCtrlDown(e));
+                }
+
+                @Override
+                public void keyReleased(KeyEvent e) {
+                    update(isCtrlDown(e));
+                }
+            });
+        }
+
+        private static boolean isCtrlDown(InputEvent e) {
+            // Cmd on Mac OS X, Ctrl elsewhere
+            int MOD = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();
+            return (e.getModifiersEx() & MOD) != 0;
+        }
+
+        private void update(boolean modifierDown) {
+            if (!modifierDown || lastMouse == null) {
+                clear();
+                return;
+            }
+
+            int offset = offsetAt(lastMouse);
+            if (offset < 0) {
+                clear();
+                return;
+            }
+
+            try {
+                int[] startEnd = findPathBoundaries(pane, offset);

Review Comment:
   With Ctrl held, every mouse move runs several `File.exists()` calls on the 
event thread before the "unchanged" check. That's fine on local disks but may 
be noticeable on network mounts. Caching by line or offset, or checking the 
range first, would help.



##########
platform/core.output2/src/org/netbeans/core/output2/ui/AbstractOutputPane.java:
##########
@@ -1025,4 +1037,237 @@ public void setSelectionVisible(boolean vis) {
             }
         }
     }
+    
+    
+    private static class HoverSupport {
+        private final JEditorPane pane;
+
+        private Object hightlightHandle;
+        private int[] highlightRange;
+        private Point lastMouse;            // so key presses can re-evaluate 
without mouse movement
+
+        public HoverSupport(JEditorPane pane) {
+            this.pane = pane;
+
+            MouseAdapter mouse = new MouseAdapter() {
+                @Override
+                public void mouseMoved(MouseEvent e) {
+                    lastMouse = e.getPoint();
+                    update(isCtrlDown(e));
+                }
+
+                @Override
+                public void mouseDragged(MouseEvent e) {
+                    clear();
+                }
+
+                @Override
+                public void mouseExited(MouseEvent e) {
+                    lastMouse = null;
+                    clear();
+                }
+
+                @Override
+                public void mouseClicked(MouseEvent e) {
+                    if (isCtrlDown(e) && highlightRange != null) {
+                        onActivate(highlightRange[0], highlightRange[1]);
+                        e.consume();
+                    }
+                }
+            };
+            pane.addMouseListener(mouse);
+            pane.addMouseMotionListener(mouse);
+
+            pane.addKeyListener(new KeyAdapter() {
+                @Override
+                public void keyPressed(KeyEvent e) {
+                    update(isCtrlDown(e));
+                }
+
+                @Override
+                public void keyReleased(KeyEvent e) {
+                    update(isCtrlDown(e));
+                }
+            });
+        }
+
+        private static boolean isCtrlDown(InputEvent e) {
+            // Cmd on Mac OS X, Ctrl elsewhere
+            int MOD = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();
+            return (e.getModifiersEx() & MOD) != 0;
+        }
+
+        private void update(boolean modifierDown) {
+            if (!modifierDown || lastMouse == null) {
+                clear();
+                return;
+            }
+
+            int offset = offsetAt(lastMouse);
+            if (offset < 0) {
+                clear();
+                return;
+            }
+
+            try {
+                int[] startEnd = findPathBoundaries(pane, offset);
+                String path = startEnd == null ? "" : 
pane.getDocument().getText(startEnd[0], startEnd[1] - startEnd[0]);
+                if (path.isBlank()) {
+                    clear();
+                    return;
+                }
+                if (highlightRange != null && startEnd[0] == highlightRange[0] 
&& startEnd[1] == highlightRange[1]) {
+                    return;   // unchanged, avoid repaint churn
+                }
+                clear();
+                hightlightHandle = 
pane.getHighlighter().addHighlight(startEnd[0], startEnd[1], UNDERLINE);
+                highlightRange = startEnd;
+                pane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
+            } catch (BadLocationException ex) {
+                clear();
+            }
+        }
+
+        /**
+         * Document offset under the point, or -1 if the pointer isn't actually
+         * over a character.
+         */
+        private int offsetAt(Point p) {
+            Position.Bias[] bias = new Position.Bias[1];
+            int offset = pane.getUI().viewToModel2D(pane, p, bias);
+            if (offset < 0) {
+                return -1;
+            }
+            // viewToModel returns the *nearest* offset, even in empty space 
past line end — verify
+            if (bias[0] == Position.Bias.Backward && offset > 0) {
+                offset--;
+            }
+            try {
+                int len = pane.getDocument().getLength();
+                if (offset >= len) {
+                    return -1;
+                }
+                Rectangle2D a = pane.modelToView2D(offset);
+                Rectangle2D b = pane.modelToView2D(Math.min(offset + 1, len));
+                double right = (b.getY() == a.getY()) ? b.getX() : a.getX() + 
8; // char box
+                return (p.x >= a.getX() && p.x <= right
+                        && p.y >= a.getY() && p.y <= a.getY() + a.getHeight()) 
? offset : -1;
+            } catch (BadLocationException e) {
+                return -1;
+            }
+        }
+
+        private void clear() {
+            if (hightlightHandle != null) {
+                pane.getHighlighter().removeHighlight(hightlightHandle);
+                hightlightHandle = null;
+            }
+            highlightRange = null;
+            pane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
+        }
+
+        private void onActivate(int start, int end) {
+            try {
+                String path = pane.getText(start, end - start);
+                File file = new File(path);
+                if (file.exists()) {
+                    if (FileUtil.toFileObject(file) instanceof FileObject 
toOpen) {
+                        if (toOpen.getLookup().lookup(Openable.class) 
instanceof Openable doIt) {
+                            doIt.open();
+                            return;
+                        }
+                    }
+                }
+            } catch (BadLocationException ex) {
+                ex.printStackTrace();
+            }
+            Toolkit.getDefaultToolkit().beep();
+        }
+
+        /**
+         * Painter that draws an underline instead of filling the background.
+         */
+        private static final Highlighter.HighlightPainter UNDERLINE
+                = new LayeredHighlighter.LayerPainter() {
+            @Override
+            public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds,
+                    JTextComponent c, View view) {
+                try {
+                    Shape s = view.modelToView(p0, Position.Bias.Forward,
+                            p1, Position.Bias.Backward, bounds);
+                    Rectangle r = s.getBounds();
+                    g.setColor(new Color(0x2060D0));
+                    int y = r.y + r.height - 2;
+                    g.drawLine(r.x, y, r.x + r.width - 1, y);
+                    return r;
+                } catch (BadLocationException e) {
+                    return null;
+                }
+            }
+
+            @Override
+            public void paint(Graphics g, int p0, int p1, Shape bounds, 
JTextComponent c) {
+                // not used: DefaultHighlighter calls paintLayer for 
LayerPainters
+            }
+        };
+
+        private static int[] findPathBoundaries(JEditorPane pane, int offset) 
throws BadLocationException {
+            int start = Utilities.getWordStart(pane, offset);
+            int end = Utilities.getWordEnd(pane, offset);
+            
+            Set<String> candidates = new TreeSet<>((s1, s2) -> {
+                int lenDiff = s2.length() - s1.length();
+                if (lenDiff != 0) {
+                    return lenDiff;
+                } else {
+                    return s2.compareTo(s1);
+                }
+            });
+            while (start < end) {
+                candidates.add(pane.getText(start, end - start));
+                start--;
+                if (start < 0 || !isPathSeparator(pane.getText(start, 1))) {
+                    break;
+                } else {
+                    candidates.add(pane.getText(start, end - start));
+                    start = Utilities.getPreviousWord(pane, start);
+                }
+            }
+            
+            File existingPrefix = null;
+            for (String path : candidates) {
+                File f = new File(path).getAbsoluteFile();

Review Comment:
   `getAbsoluteFile()` resolves relative paths against the IDE's `user.dir`, 
not the working directory of the process that printed the output. So a relative 
path from a Maven or Ant build will usually not be found, or worse, will match 
an unrelated file. Should relative paths be resolved against the working 
directory of the process behind the `InputOutput`, if one is known?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to