Commit 0a80ce1e authored by John Stowers's avatar John Stowers

Consider redefined ref commands in validator

parent da104f1d
......@@ -114,8 +114,8 @@ class LaTeXCompletionHandler(ICompletionHandler):
def __init__(self):
self._log.debug("init")
self._language_model = LanguageModelFactory().create_language_model()
#get the language_model singleton
self._language_model = LanguageModelFactory().get_language_model()
self._bibtex_document_cache = BibTeXDocumentCache()
def set_outline(self, outline):
......@@ -219,9 +219,6 @@ class LaTeXCompletionHandler(ICompletionHandler):
return []
def __del__(self):
self._log.debug("Properly destroyed %s" % self)
from ..preferences import Preferences
from . import LaTeXSource
......
......@@ -24,6 +24,8 @@ latex.editor
BENCHMARK = True
from copy import deepcopy
from gi.repository import Gtk
from gi.repository import Gdk
from logging import getLogger
......@@ -32,15 +34,14 @@ if BENCHMARK: import time
from ..base.editor import Editor
from ..base.file import File
from completion import LaTeXCompletionHandler
from ..issues import Issue, IIssueHandler
from ..util import verbose, open_error
from copy import deepcopy
from parser import LaTeXParser
from expander import LaTeXReferenceExpander
from outline import LaTeXOutlineGenerator
from validator import LaTeXValidator
from completion import LaTeXCompletionHandler
from dialogs import ChooseMasterDialog
......@@ -59,7 +60,6 @@ class LaTeXEditor(Editor, IIssueHandler):
@property
def completion_handlers(self):
self.__latex_completion_handler = LaTeXCompletionHandler()
return [ self.__latex_completion_handler ]
def init(self, file, context):
......
......@@ -126,19 +126,30 @@ class Placeholder(Element):
children = property(get_children, set_children)
#LanguageModel is pickled to save startup time...
#using pickle is dangerous. If you add or change the instance members of this class
#that need to persis through pickle, then INCREMENT THE VERSION
LANGUAGE_MODEL_VERSION = 1
class LanguageModel(object):
"""
"""
REF_CMDS = set(("ref","eqref","pageref"))
__log = getLogger("LanguageModel")
def __init__(self):
self.commands = {} # maps command names to Command elements
self.VERSION = LANGUAGE_MODEL_VERSION
self.__placeholders = {}
self.__newcommands = []
self.__log.debug("init")
#some latex specific helpers.
self.__new_ref_commands = {}
self.__log.debug("LanguageModel init")
def find_command(self, prefix):
"""
......@@ -169,11 +180,17 @@ class LanguageModel(object):
except KeyError:
self.__log.error("fill_placeholder: placeholder '%s' not registered" % name)
def is_ref_command(self, cmd_name):
return (cmd_name in self.REF_CMDS) or (cmd_name in self.__new_ref_commands)
def set_newcommands(self, outlinenodes):
self.__log.debug("Set newcommands")
self.__log.debug("set newcommands: %s" % ",".join([o.value for o in outlinenodes]))
#remove old state
self.__new_ref_commands = {}
for name in self.__newcommands:
self.commands.__delitem__(name)
del(self.commands[name])
self.__newcommands = []
for o in outlinenodes:
#if this is a redefinition of an existing node then use that node as
......@@ -184,13 +201,21 @@ class LanguageModel(object):
#the display name
old = copy.copy(self.commands[o.oldcmd])
old.name = o.value
self.commands[o.value] = old
self.__newcommands.append(o.value)
if o.oldcmd in self.REF_CMDS:
self.__new_ref_commands[o.value] = 1
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
self.__newcommands.append(command.name)
from xml import sax
......@@ -277,16 +302,19 @@ class LanguageModelFactory(object):
pickled_object = self.__find_pickled_object()
if pickled_object:
self.__language_model = pickled_object
self.__log.debug("Pickled object loaded")
self.language_model = pickled_object
else:
self.__log.debug("No pickled object loaded")
pkl_filename = Resources().get_user_file("latex.pkl")
xml_filename = Resources().get_data_file("latex.xml")
self.__language_model = LanguageModel()
self.language_model = LanguageModel()
parser = LanguageModelParser()
parser.parse(xml_filename, self.__language_model)
parser.parse(xml_filename, self.language_model)
pickle.dump(self.__language_model, open(pkl_filename, 'w'))
pickle.dump(self.language_model, open(pkl_filename, 'w'))
self.__log.info("Pickling language model")
self._ready = True
......@@ -299,18 +327,17 @@ class LanguageModelFactory(object):
self.__log.debug("Pickled object and XML file have different modification times")
else:
try:
self.__log.debug("Pickled object found: %s" % pkl_file.path)
return pickle.load(open(pkl_file.path))
obj = pickle.load(open(pkl_file.path))
if obj.VERSION == LANGUAGE_MODEL_VERSION:
return obj
else:
self.__log.info("Language model obsolete")
except:
return None
else:
self.__log.debug("No pickled object found")
self.__log.info("Invalid language model", exc_info=True)
return None
def create_language_model(self):
"""
Return a new LanguageModel
"""
return deepcopy(self.__language_model)
def get_language_model(self):
return self.language_model
# ex:ts=4:et:
......@@ -61,15 +61,21 @@ class OutlineNode(list):
class Outline(object):
def __init__(self):
self.rootNode = OutlineNode(OutlineNode.ROOT, level=0)
self.labels = [] # OutlineNode objects
self.bibliographies = [] # File objects
self.colors = []
self.packages = [] # OutlineNode objects
self.packages = [] # OutlineNode objects
self.newcommands = [] # OutlineNode objects
self.newenvironments = [] # OutlineNode objects
self.new_ref_commands = {}
REF_CMDS = set(("ref","eqref","pageref"))
def is_ref_command(self, cmd_name):
return (cmd_name in REF_CMDS) or (cmd_name in self.new_ref_commands)
from ..base.file import File
from ..preferences import Preferences
......@@ -271,11 +277,5 @@ class LaTeXOutlineGenerator(object):
self._walk(node, issue_handler, childForeign)
#~ def __del__(self):
#~ print "Properly destroyed %s" % self
# ex:ts=4:et:
......@@ -29,8 +29,10 @@ from os.path import exists
from ..base.file import File
from ..issues import Issue
from ..util import escape
from parser import Node
from environment import Environment
from model import LanguageModelFactory
LOG = getLogger(__name__)
......@@ -45,6 +47,8 @@ class LaTeXValidator(object):
def __init__(self):
self._environment = Environment()
#the the language_model singleton
self._language_model = LanguageModelFactory().get_language_model()
def validate(self, document_node, outline, issue_handler, document_preferences):
"""
......@@ -131,7 +135,7 @@ class LaTeXValidator(object):
except IndexError:
issue_handler.issue(Issue("Environment <b>%s</b> has no beginning" % escape(environ), node.start, node.end, node.file, Issue.SEVERITY_ERROR))
elif node.value == "ref" or node.value == "eqref" or node.value == "pageref":
elif self._language_model.is_ref_command(node.value):
# mark label as used
try:
label = node.firstOfType(Node.MANDATORY_ARGUMENT).innerText
......
......@@ -42,12 +42,16 @@ I like \cite{dijkstra76}
\label{fig:fixme}
\end{figure}
\section{cake}
\subsection{chips}
\subsubsection{candy}
\section{cake}\label{sec:foo}
\subsection{chips}\label{sec:bar}
\subsubsection{candy}\label{sec:baz}
lets use a \testcite{dijkstra68}
see \ref{sec:foo}.
also see \testref{sec:bar}
\include{chap2}
\include{chap3}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment