Skip to content

Commit

Permalink
Fixed #801: language keywords are now set using classprefs only
Browse files Browse the repository at this point in the history
* replaced SetKeyWordsFromDict with SetKeyWordsFromClassprefs
* added keyword_set_[0-7] to FundamentalMode classprefs, defaulting to hidden
* added keyword_set_* classprefs to each mode that has keywords
* updated major modes using make-major-modes-from-editra.py to add keywords to classprefs defaults

svn-revision: r2413
  • Loading branch information
robmcmullen committed Nov 30, 2009
1 parent 44c06a9 commit 60a7d87
Show file tree
Hide file tree
Showing 82 changed files with 355 additions and 79 deletions.
132 changes: 77 additions & 55 deletions editra.in/make-major-modes-from-editra.py
Expand Up @@ -25,6 +25,7 @@


classprefs_template = ''' StrParam('extensions', '%(extensions)s', fullwidth=True),'''
keyword_set_template = ''' StrParam('keyword_set_%d', unique_keywords[%d], hidden=False, fullwidth=True),'''


template = '''# peppy Copyright (c) 2006-2009 Rob McMullen
Expand All @@ -49,6 +50,7 @@
from peppy.yapsy.plugins import *
from peppy.major import *
from peppy.fundamental import FundamentalMode
from peppy.editra.style_specs import unique_keywords
class %(class_name)sMode(FundamentalMode):
"""Stub major mode for editing %(keyword)s files.
Expand All @@ -73,6 +75,55 @@ def getMajorModes(self):
yield %(class_name)sMode
'''

# Global lists and dicts using my multiple processes
langs = facade.getAllEditraLanguages()
extra_properties = {}
syntax_style_specs = {}
keywords = {}
stc_lexer_id = {}
for lang in langs:
keyword = facade.getPeppyModeKeyword(lang)
#dprint(keyword)
extra_properties[keyword] = facade.getEditraExtraProperties(lang)
syntax_style_specs[keyword] = facade.getEditraSyntaxSpecs(lang)
keywords[keyword] = facade.getEditraLanguageKeywords(lang)
stc_lexer_id[keyword] = facade.getEditraSTCLexer(lang)

def findCommonKeywords(keywords):
unique_keywords = []
unique_id = 0
keywords_text = {}
keywords_mapping = {}
for lang, keyword_dict in keywords.iteritems():
if lang not in keywords_mapping:
dprint("adding %s" % lang)
keywords_mapping[lang] = {}
try:
for id, text in keyword_dict.iteritems():
# keyword_spec is a tuple of int and string
if text in keywords_text:
dprint("found common for %s, %d: %s" % (lang, id, keywords_text[text]))
keywords_mapping[lang][id] = keywords_text[text]
else:
keywords_text[text] = unique_id
unique_keywords.append(text)
keywords_mapping[lang][id] = unique_id
unique_id += 1
except (ValueError, TypeError):
dprint(lang)
dprint(keyword_spec_list)
raise
except KeyError:
dprint(keywords_mapping.keys())
raise
dprint(keywords_mapping)
return unique_keywords, keywords_mapping

unique_keywords, keywords_mapping = findCommonKeywords(keywords)



# Processing routines

def process(destdir):
missing, existing = getDefinedModes(destdir)
Expand All @@ -99,11 +150,10 @@ def getDefinedModes(destdir):
def getEditraInfo(lang):
module_name = facade.getPeppyFileName(lang)
syn = facade.getEditraSyntaxData(lang)
# if lang == "XML":
# dprint(syn)
keyword = facade.getPeppyModeKeyword(lang)
vals = {
'lang': lang,
'keyword': facade.getPeppyModeKeyword(lang),
'keyword': keyword,
'class_name': facade.getPeppyClassName(lang),
'module_name': module_name,
'extensions': " ".join(facade.getExtensionsForLanguage(lang)),
Expand All @@ -112,7 +162,12 @@ def getEditraInfo(lang):
'end_comment': repr(facade.getEditraCommentChars(lang)[1]),
}
vals['class_attrs'] = class_attr_template % vals
vals['classprefs'] = classprefs_template % vals
classprefs = classprefs_template % vals
order = sorted(keywords_mapping[keyword].iteritems())
for keyword_set_id, unique_id in order:
classprefs += "\n" + keyword_set_template % (keyword_set_id, unique_id)

vals['classprefs'] = classprefs
return module_name, vals

def convertEditraMode(destdir, lang):
Expand Down Expand Up @@ -186,9 +241,27 @@ def replace(self, vals):
"""Replace any class attributes or classprefs with the new values
"""
self.replaceImports()
self.replaceClassAttrs(vals)
self.replaceClassprefs(vals)

def replaceImports(self):
"""Special case to add the unique_keywords dict to the list of imports
In versions prior to r2412, the import statement for unique_keywords
from peppy.editra.style_specs didn't exist. Now that keywords can be
modified by the user, the import statement must be included because
the StrParams reference the unique_keywords dict to supply defaults
for the preferences.
"""
extra = "from peppy.editra.style_specs import unique_keywords"
try:
self.header.index(extra)
except ValueError:
dprint("Replacing imports for %s" % self.lang)
index = self.header.index("from peppy.fundamental import")
self.header = self.header[0:index] + extra + "\n" + self.header[index:]

def replaceClassAttrs(self, vals):
newattrs = vals['class_attrs']
keywords = {}
Expand Down Expand Up @@ -264,27 +337,6 @@ def processSampleText(filename):

def processStyleSpecs(filename):
#dprint("Processing style specs")
langs = facade.getAllEditraLanguages()
extra_properties = {}
syntax_style_specs = {}
keywords = {}
stc_lexer_id = {}
for lang in langs:
keyword = facade.getPeppyModeKeyword(lang)
#dprint(keyword)
extra_properties[keyword] = facade.getEditraExtraProperties(lang)
syntax_style_specs[keyword] = facade.getEditraSyntaxSpecs(lang)
keywords[keyword] = facade.getEditraLanguageKeywords(lang)
stc_lexer_id[keyword] = facade.getEditraSTCLexer(lang)

# for lang in langs:
# keyword = facade.getPeppyModeKeyword(lang)
# dprint(keyword)
# dprint(keywords[keyword])
# sys.exit()

unique_keywords, keywords_mapping = findCommonKeywords(keywords)

import pprint
pp = pprint.PrettyPrinter()
fh = open(filename, "w")
Expand All @@ -303,36 +355,6 @@ def processStyleSpecs(filename):
fh.write("\n")
fh.close()

def findCommonKeywords(keywords):
unique_keywords = []
unique_id = 0
keywords_text = {}
keywords_mapping = {}
for lang, keyword_dict in keywords.iteritems():
if lang not in keywords_mapping:
dprint("adding %s" % lang)
keywords_mapping[lang] = {}
try:
for id, text in keyword_dict.iteritems():
# keyword_spec is a tuple of int and string
if text in keywords_text:
dprint("found common for %s, %d: %s" % (lang, id, keywords_text[text]))
keywords_mapping[lang][id] = keywords_text[text]
else:
keywords_text[text] = unique_id
unique_keywords.append(text)
keywords_mapping[lang][id] = unique_id
unique_id += 1
except (ValueError, TypeError):
dprint(lang)
dprint(keyword_spec_list)
raise
except KeyError:
dprint(keywords_mapping.keys())
raise
dprint(keywords_mapping)
return unique_keywords, keywords_mapping



if __name__ == "__main__":
Expand Down
35 changes: 11 additions & 24 deletions peppy/editra/stcmixin.py
Expand Up @@ -181,15 +181,7 @@ def ConfigureLexer(self, keyword):
self.SetLexer(lexer)

# Set or clear keywords used for extra highlighting
if self.stc_keywords is not None:
keywords = self.stc_keywords
else:
try:
keywords = style_specs.keywords_mapping[keyword]
except KeyError:
dprint("No keywords found for %s" % keyword)
keywords = []
self.SetKeyWordsFromDict(keywords)
self.SetKeyWordsFromClassprefs()

# Set or clear Editra style sheet info
if self.stc_syntax_style_specs is not None:
Expand Down Expand Up @@ -218,23 +210,18 @@ def ConfigureLexer(self, keyword):
self.dprint("GetLexer = %d" % self.GetLexer())
return True

def SetKeyWordsFromDict(self, kw_dict):
"""Sets the keywords from a dict of keyword sets
@param kw_dict: {KWSET1: "KEWORDS", KWSET2: "KEYWORDS2", etc...]
def SetKeyWordsFromClassprefs(self):
"""Sets the keywords from the classprefs
"""
# Parse Keyword Settings List simply ignoring bad values and badly
# formed lists
import peppy.editra.style_specs as style_specs

self.keywords = ""
for keyword_set, keywords in kw_dict.iteritems():
# If the keyword is an integer, assume that it references the
# list of unique keywords specified in the preprocessed editra
# style_specs
if isinstance(keywords, int):
keywords = style_specs.unique_keywords[keywords]
self.keywords += keywords
self.SetKeyWords(keyword_set, keywords)
prefname_template = "keyword_set_%d"
for set_num in range(8):
prefname = prefname_template % set_num
keywords = self.classprefs._get(prefname)
dprint("set number: %d keywords=%s" % (set_num, keywords))
if keywords:
self.keywords += keywords
self.SetKeyWords(set_num, keywords)

kwlist = self.keywords.split() # Split into a list of words
kwlist = list(set(kwlist)) # Uniqueify the list
Expand Down
8 changes: 8 additions & 0 deletions peppy/fundamental.py
Expand Up @@ -158,6 +158,14 @@ class FundamentalMode(FoldExplorerMixin, EditraSTCMixin,
BoolParam('case_sensitive_search', False, 'Case of search string must match exactly if True; otherwise mixed case requires exact match and lower case matches all', local=True),
BoolParam('whole_word_search', False, 'Whole word between common separators must match if True; Otherwise matches every substring', local=True),
BoolParam('case_matching_replace', True, 'Case will be modified to match if True; otherwise case will be left as was found', local=True),
StrParam('keyword_set_0', "", 'Space separated list of keywords used for scintilla keyword set 0', hidden=True),
StrParam('keyword_set_1', "", 'Space separated list of keywords used for scintilla keyword set 1', hidden=True),
StrParam('keyword_set_2', "", 'Space separated list of keywords used for scintilla keyword set 2', hidden=True),
StrParam('keyword_set_3', "", 'Space separated list of keywords used for scintilla keyword set 3', hidden=True),
StrParam('keyword_set_4', "", 'Space separated list of keywords used for scintilla keyword set 4', hidden=True),
StrParam('keyword_set_5', "", 'Space separated list of keywords used for scintilla keyword set 5', hidden=True),
StrParam('keyword_set_6', "", 'Space separated list of keywords used for scintilla keyword set 6', hidden=True),
StrParam('keyword_set_7', "", 'Space separated list of keywords used for scintilla keyword set 7', hidden=True),
)

autoindent = NullAutoindent()
Expand Down
5 changes: 5 additions & 0 deletions peppy/major_modes/_68k_assembly.py
Expand Up @@ -19,6 +19,7 @@
from peppy.lib.autoindent import *
from peppy.yapsy.plugins import *
from peppy.major import *
from peppy.editra.style_specs import unique_keywords
from peppy.fundamental import FundamentalMode

class _68kAssemblyMode(FundamentalMode):
Expand All @@ -37,6 +38,10 @@ class _68kAssemblyMode(FundamentalMode):

default_classprefs = (
StrParam('extensions', '68k', fullwidth=True),
StrParam('keyword_set_0', unique_keywords[81], hidden=False, fullwidth=True),
StrParam('keyword_set_1', unique_keywords[82], hidden=False, fullwidth=True),
StrParam('keyword_set_2', unique_keywords[83], hidden=False, fullwidth=True),
StrParam('keyword_set_3', unique_keywords[84], hidden=False, fullwidth=True),
)


Expand Down
3 changes: 3 additions & 0 deletions peppy/major_modes/actionscript.py
Expand Up @@ -19,6 +19,7 @@
from peppy.lib.autoindent import *
from peppy.yapsy.plugins import *
from peppy.major import *
from peppy.editra.style_specs import unique_keywords
from peppy.fundamental import FundamentalMode

class ActionScriptMode(FundamentalMode):
Expand All @@ -37,6 +38,8 @@ class ActionScriptMode(FundamentalMode):

default_classprefs = (
StrParam('extensions', 'as asc mx', fullwidth=True),
StrParam('keyword_set_0', unique_keywords[144], hidden=False, fullwidth=True),
StrParam('keyword_set_1', unique_keywords[145], hidden=False, fullwidth=True),
)


Expand Down
2 changes: 2 additions & 0 deletions peppy/major_modes/ada.py
Expand Up @@ -19,6 +19,7 @@
from peppy.lib.autoindent import *
from peppy.yapsy.plugins import *
from peppy.major import *
from peppy.editra.style_specs import unique_keywords
from peppy.fundamental import FundamentalMode

class AdaMode(FundamentalMode):
Expand All @@ -37,6 +38,7 @@ class AdaMode(FundamentalMode):

default_classprefs = (
StrParam('extensions', 'a ada adb ads', fullwidth=True),
StrParam('keyword_set_0', unique_keywords[24], hidden=False, fullwidth=True),
)


Expand Down
3 changes: 3 additions & 0 deletions peppy/major_modes/apache_conf.py
Expand Up @@ -19,6 +19,7 @@
from peppy.lib.autoindent import *
from peppy.yapsy.plugins import *
from peppy.major import *
from peppy.editra.style_specs import unique_keywords
from peppy.fundamental import FundamentalMode

class ApacheConfMode(FundamentalMode):
Expand All @@ -37,6 +38,8 @@ class ApacheConfMode(FundamentalMode):

default_classprefs = (
StrParam('extensions', 'conf htaccess', fullwidth=True),
StrParam('keyword_set_0', unique_keywords[16], hidden=False, fullwidth=True),
StrParam('keyword_set_1', unique_keywords[17], hidden=False, fullwidth=True),
)


Expand Down
2 changes: 2 additions & 0 deletions peppy/major_modes/bash.py
Expand Up @@ -14,6 +14,7 @@
from peppy.lib.autoindent import *
from peppy.yapsy.plugins import *
from peppy.major import *
from peppy.editra.style_specs import unique_keywords
from peppy.fundamental import FundamentalMode

class BashMode(SimpleFoldFunctionMatchMixin, FundamentalMode):
Expand All @@ -31,6 +32,7 @@ class BashMode(SimpleFoldFunctionMatchMixin, FundamentalMode):

default_classprefs = (
StrParam('extensions', 'bsh configure sh', fullwidth=True),
StrParam('keyword_set_0', unique_keywords[74], hidden=False, fullwidth=True),
Param('indent_after', r'(\{(?![^\}]*\})|\b(then|elif|else)\b(?!.+fi)|\bdo\b(?!.+done)|\bcase\s+.+\s+in\b(?!.*esac)|\[\[)', fullwidth=True),
Param('indent', r'\$\{.*\}', fullwidth=True),
Param('unindent', r'([}]\s*$|\b(fi|elif|else)\b|\bdone\b|\besac\b|\]\])', fullwidth=True),
Expand Down
2 changes: 2 additions & 0 deletions peppy/major_modes/boo.py
Expand Up @@ -19,6 +19,7 @@
from peppy.lib.autoindent import *
from peppy.yapsy.plugins import *
from peppy.major import *
from peppy.editra.style_specs import unique_keywords
from peppy.fundamental import FundamentalMode

class BooMode(FundamentalMode):
Expand All @@ -37,6 +38,7 @@ class BooMode(FundamentalMode):

default_classprefs = (
StrParam('extensions', 'boo', fullwidth=True),
StrParam('keyword_set_0', unique_keywords[15], hidden=False, fullwidth=True),
)


Expand Down
4 changes: 4 additions & 0 deletions peppy/major_modes/c.py
Expand Up @@ -15,6 +15,7 @@
from peppy.lib.autoindent import CStyleAutoindent
from peppy.yapsy.plugins import *
from peppy.major import *
from peppy.editra.style_specs import unique_keywords
from peppy.fundamental import FundamentalMode, ParagraphInfo
from peppy.paragraph import *

Expand Down Expand Up @@ -92,6 +93,9 @@ class CMode(SimpleCLikeFoldFunctionMatchMixin, FundamentalMode):

default_classprefs = (
StrParam('extensions', 'c h', fullwidth=True),
StrParam('keyword_set_0', unique_keywords[79], hidden=False, fullwidth=True),
StrParam('keyword_set_1', unique_keywords[80], hidden=False, fullwidth=True),
StrParam('keyword_set_2', unique_keywords[14], hidden=False, fullwidth=True),
)

autoindent = CStyleAutoindent()
Expand Down
4 changes: 4 additions & 0 deletions peppy/major_modes/caml.py
Expand Up @@ -19,6 +19,7 @@
from peppy.lib.autoindent import *
from peppy.yapsy.plugins import *
from peppy.major import *
from peppy.editra.style_specs import unique_keywords
from peppy.fundamental import FundamentalMode

class CamlMode(FundamentalMode):
Expand All @@ -37,6 +38,9 @@ class CamlMode(FundamentalMode):

default_classprefs = (
StrParam('extensions', 'ml mli', fullwidth=True),
StrParam('keyword_set_0', unique_keywords[109], hidden=False, fullwidth=True),
StrParam('keyword_set_1', unique_keywords[110], hidden=False, fullwidth=True),
StrParam('keyword_set_2', unique_keywords[111], hidden=False, fullwidth=True),
)


Expand Down

0 comments on commit 60a7d87

Please sign in to comment.