Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 14 additions & 4 deletions preditor/gui/codehighlighter.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from __future__ import absolute_import

import json
import keyword
import os
import re

from Qt.QtGui import QColor, QSyntaxHighlighter, QTextCharFormat

from .. import resourcePath
from .. import resourcePath, utils


class CodeHighlighter(QSyntaxHighlighter):
Expand All @@ -28,6 +27,8 @@ def initHighlightVariables(self):
# comment, do not highlight it).
self.spans = []

self._enabled = True

# Language specific lists
self._comments = []
self._keywords = []
Expand All @@ -52,11 +53,17 @@ def initHighlightVariables(self):
self._resultColor = QColor(125, 128, 128)
self._stringColor = QColor(255, 128, 0)

def enabled(self):
return self._enabled

def setEnabled(self, state):
self._enabled = state

def setLanguage(self, lang):
"""Sets the language of the highlighter by loading the json definition"""
filename = resourcePath('lang/%s.json' % lang.lower())
if os.path.exists(filename):
data = json.load(open(filename))
data = utils.Json(filename).load()
self.setObjectName(data.get('name', ''))

self._comments = data.get('comments', [])
Expand Down Expand Up @@ -91,6 +98,9 @@ def highlightBlock(self, text):
"""Highlights the inputed text block based on the rules of this code
highlighter"""

if not self.enabled():
return

# Reset the highlight spans for this text block
self.spans = []

Expand Down Expand Up @@ -131,7 +141,7 @@ def highlightText(self, text, expr, format):

Args:
text (str): text to highlight
expr (QRegularExpression): search parameter
expr (re.compile): search parameter
format (QTextCharFormat): formatting rule
"""
if expr is None or not text:
Expand Down
48 changes: 32 additions & 16 deletions preditor/gui/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ def __init__(self, parent):
self.addAction(self.uiClearToLastPromptACT)

self.x = 0
self.clickPos = None
self.anchor = None
self.mousePressPos = None

# Make sure console cursor is visible. It can get it's width set to 0 with
# unusual(ie not 100%) os display scaling.
Expand Down Expand Up @@ -220,30 +219,47 @@ def setConsoleFont(self, font):
if origPercent is not None:
self.doubleSingleShotSetScrollValue(origPercent)

def mouseMoveEvent(self, event):
"""Overload of mousePressEvent to change mouse pointer to indicate it is
over a clickable error hyperlink.
"""
if self.anchorAt(event.pos()):
QApplication.setOverrideCursor(Qt.CursorShape.PointingHandCursor)
else:
QApplication.restoreOverrideCursor()
return super().mouseMoveEvent(event)

def mousePressEvent(self, event):
"""Overload of mousePressEvent to capture click position, so on release, we can
check release position. If it's the same (ie user clicked vs click-drag to
select text), we check if user clicked an error hyperlink.
"""
self.clickPos = event.pos()
self.anchor = self.anchorAt(event.pos())
if self.anchor:
QApplication.setOverrideCursor(Qt.CursorShape.PointingHandCursor)
return super(ConsolePrEdit, self).mousePressEvent(event)
left = event.button() == Qt.MouseButton.LeftButton
anchor = self.anchorAt(event.pos())
self.mousePressPos = event.pos()

if left and anchor:
event.ignore()
return

return super().mousePressEvent(event)

def mouseReleaseEvent(self, event):
"""Overload of mouseReleaseEvent to capture if user has left clicked... Check if
click position is the same as release position, if so, call errorHyperlink.
"""
samePos = event.pos() == self.clickPos
samePos = event.pos() == self.mousePressPos
left = event.button() == Qt.MouseButton.LeftButton
if samePos and left and self.anchor:
self.errorHyperlink()
anchor = self.anchorAt(event.pos())

if samePos and left and anchor:
self.errorHyperlink(anchor)
self.mousePressPos = None

self.clickPos = None
self.anchor = None
QApplication.restoreOverrideCursor()
return super(ConsolePrEdit, self).mouseReleaseEvent(event)
ret = super(ConsolePrEdit, self).mouseReleaseEvent(event)

return ret

def keyReleaseEvent(self, event):
"""Override of keyReleaseEvent to determine when to end navigation of
Expand All @@ -254,7 +270,7 @@ def keyReleaseEvent(self, event):
else:
event.ignore()

def errorHyperlink(self):
def errorHyperlink(self, anchor):
"""Determine if chosen line is an error traceback file-info line, if so, parse
the filepath and line number, and attempt to open the module file in the user's
chosen text editor at the relevant line, using specified Command Prompt pattern.
Expand All @@ -267,13 +283,13 @@ def errorHyperlink(self):
doHyperlink = (
hasattr(window, 'uiErrorHyperlinksCHK')
and window.uiErrorHyperlinksCHK.isChecked()
and self.anchor
and anchor
)
if not doHyperlink:
return

# info is a comma separated string, in the form: "filename, workboxIdx, lineNum"
info = self.anchor.split(', ')
info = anchor.split(', ')
modulePath = info[0]
workboxName = info[1]
lineNum = info[2]
Expand Down
10 changes: 10 additions & 0 deletions preditor/gui/find_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def find(self):
self.finder.callback_matching = self.insert_found_text
self.finder.callback_non_matching = self.insert_text

# Start fresh output line.
window = self.parent().window() if self.parent() else None
if window:
window.console().startPrompt("")

self.insert_text(self.finder.title())

self.match_files_count = 0
Expand All @@ -91,6 +96,11 @@ def find(self):
)
)

# If user has Auto-prompt chosen, do so now.
if window:
if window.uiAutoPromptCHK.isChecked():
window.console().startInputLine()

def find_in_editor(self, editor, path):
# Ensure the editor text is loaded and get its raw text
editor.__show__()
Expand Down
Loading