Skip to content

Commit

Permalink
Navigate to heading when a tag is clicked for editing (#1983)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo committed Jul 21, 2024
1 parent 826e805 commit 17b3b2b
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions novelwriter/guimain.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,14 @@ def closeDocument(self, beforeOpen: bool = False) -> None:
self.novelView.setActiveHandle(None)
return

def openDocument(self, tHandle: str | None, tLine: int | None = None,
changeFocus: bool = True, doScroll: bool = False) -> bool:
def openDocument(
self,
tHandle: str | None,
tLine: int | None = None,
sTitle: str | None = None,
changeFocus: bool = True,
doScroll: bool = False
) -> bool:
"""Open a specific document, optionally at a given line."""
if not SHARED.hasProject:
logger.error("No project open")
Expand All @@ -537,9 +543,12 @@ def openDocument(self, tHandle: str | None, tLine: int | None = None,
logger.debug("Requested item '%s' is not a document", tHandle)
return False

if sTitle and tLine is None:
if hItem := SHARED.project.index.getItemHeading(tHandle, sTitle):
tLine = hItem.line

self._changeView(nwView.EDITOR)
cHandle = self.docEditor.docHandle
if cHandle == tHandle:
if tHandle == self.docEditor.docHandle:
self.docEditor.setCursorLine(tLine)
if changeFocus:
self.docEditor.setFocus()
Expand Down Expand Up @@ -711,7 +720,6 @@ def openSelectedItem(self) -> None:
if SHARED.hasProject:
tHandle = None
sTitle = None
tLine = None
if self.projView.treeHasFocus():
tHandle = self.projView.getSelectedHandle()
elif self.novelView.treeHasFocus():
Expand All @@ -722,11 +730,8 @@ def openSelectedItem(self) -> None:
logger.warning("No item selected")
return

if tHandle and sTitle:
if hItem := SHARED.project.index.getItemHeading(tHandle, sTitle):
tLine = hItem.line
if tHandle:
self.openDocument(tHandle, tLine=tLine, changeFocus=False, doScroll=False)
self.openDocument(tHandle, sTitle=sTitle, changeFocus=False, doScroll=False)

return

Expand Down Expand Up @@ -1118,7 +1123,7 @@ def _followTag(self, tag: str, mode: nwDocMode) -> None:
tHandle, sTitle = self._getTagSource(tag)
if tHandle is not None:
if mode == nwDocMode.EDIT:
self.openDocument(tHandle)
self.openDocument(tHandle, sTitle=sTitle)
elif mode == nwDocMode.VIEW:
self.viewDocument(tHandle=tHandle, sTitle=sTitle)
return
Expand All @@ -1137,11 +1142,7 @@ def _openDocument(self, tHandle: str, mode: nwDocMode, sTitle: str, setFocus: bo
"""Handle an open document request."""
if tHandle is not None:
if mode == nwDocMode.EDIT:
tLine = None
hItem = SHARED.project.index.getItemHeading(tHandle, sTitle)
if hItem is not None:
tLine = hItem.line
self.openDocument(tHandle, tLine=tLine, changeFocus=setFocus)
self.openDocument(tHandle, sTitle=sTitle, changeFocus=setFocus)
elif mode == nwDocMode.VIEW:
self.viewDocument(tHandle=tHandle, sTitle=sTitle)
return
Expand Down

0 comments on commit 17b3b2b

Please sign in to comment.