[gedit-latex] Handle newcommands



commit 5723c77385a57e60f2d054e9fc951c97c302a911
Author: John Stowers <john stowers gmail com>
Date:   Thu Jun 30 00:14:57 2011 +1200

    Handle newcommands
    
    https://bugzilla.gnome.org/show_bug.cgi?id=653693
    
    This extends the language model / outliner to recognise the common
    case of a \newcommand (with one argument) simply redefining another
    existing command, for example \newcommand{\Secref}[1]{Section \ref{#1}}.
    
    In this case the completion for the original \ref command is offered

 latex/latex/completion.py |    8 +-------
 latex/latex/model.py      |   28 +++++++++++++++++++---------
 latex/latex/outline.py    |   21 +++++++++++++++++++--
 3 files changed, 39 insertions(+), 18 deletions(-)
---
diff --git a/latex/latex/completion.py b/latex/latex/completion.py
index ed56d9c..b7a82f2 100644
--- a/latex/latex/completion.py
+++ b/latex/latex/completion.py
@@ -133,13 +133,7 @@ class LaTeXCompletionHandler(ICompletionHandler):
         self._language_model.fill_placeholder("Colors", color_choices)
 
         # newcommands
-        newcommands = []
-        for n in outline.newcommands:
-            command = Command(None, n.value)
-            for i in range(n.numOfArgs):
-                command.children.append(MandatoryArgument(None, "#%s" % (i + 1)))
-            newcommands.append(command)
-        self._language_model.set_newcommands(newcommands)
+        self._language_model.set_newcommands(outline.newcommands)
 
         # newenvironments
         newenvironments = []
diff --git a/latex/latex/model.py b/latex/latex/model.py
index eff1931..b7f19f6 100644
--- a/latex/latex/model.py
+++ b/latex/latex/model.py
@@ -24,6 +24,7 @@ latex.model
 The LaTeX language model used for code completion.
 """
 
+import copy
 from logging import getLogger
 
 from ..base.resources import Resources
@@ -168,22 +169,31 @@ class LanguageModel(object):
         except KeyError:
             self.__log.error("fill_placeholder: placeholder '%s' not registered" % name)
 
-    def set_newcommands(self, newcommands):
-
-        # TODO: use sets
-
-        self.__log.debug("set_newcommands: " + ",".join([c.name for c in newcommands]))
+    def set_newcommands(self, outlinenodes):
 
+        self.__log.debug("set newcommands: %s" % ",".join([o.value for o in outlinenodes]))
         for name in self.__newcommands:
             self.commands.__delitem__(name)
 
-        for command in newcommands:
-            self.commands[command.name] = command
-
+        for o in outlinenodes:
+            #if this is a redefinition of an existing node then use that node as
+            #the completion helper
+            if o.oldcmd and o.oldcmd in self.commands:
+                self.__log.info("Detected redefined \\newcommand: %s -> %s" % (o.value, o.oldcmd))
+                #copy the old command so we retain its argument completion but change
+                #the display name
+                old = copy.copy(self.commands[o.oldcmd])
+                old.name = o.value
+                self.commands[o.value] = old
+            else:
+                #add a generic completer
+                command = Command(None, o.value)
+                for i in range(o.numOfArgs):
+                    command.children.append(MandatoryArgument(None, "#%s" % (i + 1)))
+                self.commands[command.name] = command
 
 from xml import sax
 
-
 class LanguageModelParser(sax.ContentHandler):
     """
     SAX parser for the language model in latex.xml
diff --git a/latex/latex/outline.py b/latex/latex/outline.py
index 473fa1b..5b1e4dc 100644
--- a/latex/latex/outline.py
+++ b/latex/latex/outline.py
@@ -36,7 +36,7 @@ class OutlineNode(list):
 
     ROOT, STRUCTURE, LABEL, NEWCOMMAND, REFERENCE, GRAPHICS, PACKAGE, TABLE, NEWENVIRONMENT = range(9)
 
-    def __init__(self, type, start=None, end=None, value=None, level=None, foreign=False, numOfArgs=None, file=None):
+    def __init__(self, type, start=None, end=None, value=None, level=None, foreign=False, numOfArgs=None, file=None, **kwargs):
         """
         numOfArgs        only used for NEWCOMMAND type
         """
@@ -49,6 +49,8 @@ class OutlineNode(list):
         self.numOfArgs = numOfArgs
         self.file = file
 
+        self.oldcmd = kwargs.get("oldcmd")
+
     @property
     def xml(self):
         if self.type == self.ROOT:
@@ -229,7 +231,22 @@ class LaTeXOutlineGenerator(object):
                         except Exception:
                             issue_handler.issue(Issue("Malformed newcommand", node.start, node.end, node.file, Issue.SEVERITY_ERROR))
                             nArgs = 0
-                        ncNode = OutlineNode(OutlineNode.NEWCOMMAND, node.start, node.lastEnd, name, numOfArgs=nArgs, file=node.file)
+
+                        #if the command has only one argument, be smart and see if it is a redefinition of an
+                        #existing latex command
+                        oldcmd = None
+                        if nArgs == 1:
+                            oldcommandnode = None
+                            newcommandsnode = node.filter(Node.MANDATORY_ARGUMENT)[1]
+                            #find the command that takes the '#1' argument
+                            for i in newcommandsnode.filter(Node.COMMAND):
+                                for j in i.filter(Node.MANDATORY_ARGUMENT):
+                                    if j.innerText == "#1":
+                                        oldcommandnode = i
+                            if oldcommandnode:
+                                oldcmd = i.value
+
+                        ncNode = OutlineNode(OutlineNode.NEWCOMMAND, node.start, node.lastEnd, name, numOfArgs=nArgs, file=node.file, oldcmd=oldcmd)
                         self._outline.newcommands.append(ncNode)
                     except IndexError:
                         issue_handler.issue(Issue("Malformed command", node.start, node.lastEnd, node.file, Issue.SEVERITY_ERROR))



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]