moving data to backend and fixing tests

This commit is contained in:
Nate Stemen 2017-10-24 10:59:41 +01:00
parent da07a91229
commit 98909026f5
9 changed files with 843 additions and 68 deletions

View file

@ -26,4 +26,3 @@ module.exports = MetaController =
docId: doc_id, meta: docMeta
}
res.sendStatus 200
MetaController

View file

@ -1,14 +1,18 @@
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
DocumentUpdaterHandler = require '../DocumentUpdater/DocumentUpdaterHandler'
packageMapping = require "./packageMapping"
module.exports = MetaHandler =
labelCaptureRegex: () ->
/\\label\{([^\}\n\\]{0,80})\}/g
labelRegex: () ->
/\\label{(.{0,80}?)}/g
packageCaptureRegex: () ->
/^\\usepackage(?:\[((?:.|\n)*?)])?\s*?{((?:.|\n)*?)}/gm
usepackageRegex: () ->
/^\\usepackage(?:\[.{0,80}?])?{(.{0,80}?)}/g
ReqPackageRegex: () ->
/^\\RequirePackage(?:\[.{0,80}?])?{(.{0,80}?)}/g
getAllMetaForProject: (projectId, callback=(err, projectMeta)->) ->
DocumentUpdaterHandler.flushProjectToMongo projectId, (err) ->
@ -31,18 +35,28 @@ module.exports = MetaHandler =
callback null, docMeta
extractMetaFromDoc: (lines) ->
docMeta = {labels: [], packages: []}
label_re = MetaHandler.labelCaptureRegex()
package_re = MetaHandler.packageCaptureRegex()
docMeta = {labels: [], packages: {}}
packages = []
label_re = MetaHandler.labelRegex()
package_re = MetaHandler.usepackageRegex()
req_package_re = MetaHandler.ReqPackageRegex()
for line in lines
while labelMatch = label_re.exec line
if labelMatch[1]
docMeta.labels.push labelMatch[1]
if label = labelMatch[1]
docMeta.labels.push label
while packageMatch = package_re.exec line
if packageMatch[2]
for pkg in packageMatch[2].split ','
if pkg.trim()
docMeta.packages.push pkg.trim()
if messy = packageMatch[1]
for pkg in messy.split ','
if clean = pkg.trim()
packages.push clean
while packageMatch = req_package_re.exec line
if messy = packageMatch[1]
for pkg in messy.split ','
if clean = pkg.trim()
packages.push clean
for pkg in packages
if packageMapping[pkg]?
docMeta.packages[pkg] = packageMapping[pkg]
return docMeta
extractMetaFromProjectDocs: (projectDocs) ->

File diff suppressed because one or more lines are too long

View file

@ -1,14 +1,8 @@
define [
"./top_hundred_snippets",
"./package_definition_snippets"
], (topHundred, packageCommandMappings) ->
"./top_hundred_snippets"
], (topHundred) ->
rawCommands = Object.keys topHundred
commandSnippets = []
for cmd, snippets of topHundred
for snippet in snippets
commandSnippets.push snippet
commandNames = (snippet.caption.match(/\w+/)[0] for snippet in topHundred)
class Parser
constructor: (@doc, @prefix) ->
@ -104,29 +98,16 @@ define [
getCompletions: (editor, session, pos, prefix, callback) ->
packages = @metadataManager.getAllPackages()
packageCommands = []
for pkg in packages
commands = packageCommandMappings[pkg]
if commands?
for command, snippets of commands
if command not in rawCommands
for snippet in snippets
packageCommands.push snippet
# for pkg in packages
# if packageCommandMappings[pkg]?
# for cmd in packageCommandMappings[pkg]
# packageCommands.push {
# caption: "\\#{cmd}"
# snippet: "\\#{cmd}"
# meta: "#{pkg}-cmd"
# score: 60
# }
for pkg, snippets of packages
for snippet in snippets
packageCommands.push snippet
doc = session.getValue()
parser = new Parser(doc, prefix)
commands = parser.parse()
completions = []
for command in commands
if command[0] not in rawCommands
if command[0] not in commandNames
caption = "\\#{command[0]}"
score = if caption == prefix then 99 else 50
snippet = caption
@ -145,7 +126,7 @@ define [
meta: "cmd"
score: score
}
completions = completions.concat commandSnippets, packageCommands
completions = completions.concat topHundred, packageCommands
callback null, completions

View file

@ -23,24 +23,34 @@ define [
range = new Range(end.row, 0, end.row, end.column)
lineUpToCursor = @editor.getSession().getTextRange range
if lineUpToCursor.trim() == '%' or lineUpToCursor.startsWith '\\'
# fix in case change is just (un)comment out a package
range = new Range end.row, 0, end.row, end.column + 80
range = new Range(end.row, 0, end.row, end.column + 80)
lineUpToCursor = @editor.getSession().getTextRange range
commandFragment = getLastCommandFragment lineUpToCursor
linesContainPackage = _.any(
change.lines,
(line) -> line.match(/\\usepackage(?:\[.*?])?\s*{.*?}/)
(line) -> line.match(/^\\usepackage(?:\[.{0,80}?])?{(.{0,80}?)}/)
)
linesContainReqPackage = _.any(
change.lines,
(line) -> line.match(/^\\RequirePackage(?:\[.{0,80}?])?{(.{0,80}?)}/)
)
linesContainLabel = _.any(
change.lines,
(line) -> line.match(/\\label\{[^\}\n\\]{0,80}\}/)
(line) -> line.match(/\\label{(.{0,80}?)}/)
)
linesContainMeta = linesContainPackage or linesContainLabel
linesContainMeta =
linesContainPackage or
linesContainLabel or
linesContainReqPackage
lastCommandFragmentIsLabel = commandFragment?.startsWith '\\label{'
lastCommandFragmentIsPackage = commandFragment?.startsWith '\\usepackage'
lastCommandFragmentIsMeta = lastCommandFragmentIsPackage or lastCommandFragmentIsLabel
lastCommandFragmentIsReqPack = commandFragment?.startsWith '\\RequirePackage'
lastCommandFragmentIsMeta =
lastCommandFragmentIsPackage or
lastCommandFragmentIsLabel or
lastCommandFragmentIsReqPack
if linesContainMeta or lastCommandFragmentIsMeta
@scheduleLoadCurrentDocMetaFromServer()
@ -49,9 +59,6 @@ define [
e.oldSession.off "change", onChange
e.session.on "change", onChange
# loadCurrentDocLabelsFromServer: () ->
# currentDocId = @$scope.docId
# @Metadata.loadDocMetaFromServer currentDocId
loadDocMetaFromServer: (docId) ->
@Metadata.loadDocMetaFromServer docId

View file

@ -6,9 +6,7 @@ define [
state = {documents: {}}
metadata = {
state: state
}
metadata = {state: state}
metadata.onBroadcastDocMeta = (data) ->
if data.docId? and data.meta?
@ -26,7 +24,11 @@ define [
_.flatten(meta.labels for docId, meta of state.documents)
metadata.getAllPackages = () ->
_.flatten(meta.packages for docId, meta of state.documents)
packageCommandMapping = {}
for _docId, meta of state.documents
for packageName, commandSnippets of meta.packages
packageCommandMapping[packageName] = commandSnippets
return packageCommandMapping
metadata.loadProjectMetaFromServer = () ->
$http

View file

@ -17,15 +17,39 @@ describe 'MetaHandler', ->
@DocumentUpdaterHandler = {
flushDocToMongo: sinon.stub()
}
@packageMapping =
foo: [
{
caption: '\\bar'
snippet: '\\bar'
meta: 'foo-cmd'
score: 12
}, {
caption: '\\bat[]{}'
snippet: '\\bar[$1]{$2}'
meta: 'foo-cmd'
score: 10
}
],
baz: [
{
caption: '\\longercommandtest{}'
snippet: '\\longercommandtest{$1}'
meta: 'baz-cmd'
score: 50
}
]
@MetaHandler = SandboxedModule.require modulePath, requires:
'../Project/ProjectEntityHandler': @ProjectEntityHandler
'../DocumentUpdater/DocumentUpdaterHandler': @DocumentUpdaterHandler
'./packageMapping': @packageMapping
describe 'extractMetaFromDoc', ->
beforeEach ->
@lines = [
'\\usepackage{foo}'
'\\usepackage{bar, baz}'
'\\usepackage{amsmath, booktabs}'
'one'
'two'
'three \\label{aaa}'
@ -38,7 +62,20 @@ describe 'MetaHandler', ->
docMeta = @MetaHandler.extractMetaFromDoc @lines
expect(docMeta).to.deep.equal {
labels: ['aaa', 'bbb']
packages: ['foo', 'bar', 'baz']
packages:
foo: [
{
caption: '\\bar'
snippet: '\\bar'
meta: 'foo-cmd'
score: 12
}, {
caption: '\\bat[]{}'
snippet: '\\bar[$1]{$2}'
meta: 'foo-cmd'
score: 10
}
]
}
describe 'extractMetaFromProjectDocs', ->
@ -60,13 +97,13 @@ describe 'MetaHandler', ->
'doc_four':
_id: 'id_four'
lines: [
'\\usepackage[foo=bar,baz=bat]{ddd}'
'\\usepackage[draft]{something}'
'\\usepackage[width=\\textwidth]{baz}'
'\\usepackage{amsmath}'
]
'doc_five':
_id: 'id_five'
lines: [
'\\usepackage{this,that}'
'\\usepackage{foo,baz}'
'\\usepackage[options=foo]{hello}'
'some text'
'\\section{this}\\label{sec:intro}'
@ -77,11 +114,41 @@ describe 'MetaHandler', ->
it 'should extract all metadata', ->
projectMeta = @MetaHandler.extractMetaFromProjectDocs @docs
expect(projectMeta).to.deep.equal {
'id_one': {labels: ['aaa'], packages: []}
'id_two': {labels: [], packages: []}
'id_three': {labels: ['bbb', 'ccc'], packages: []}
'id_four': {labels: [], packages: ['ddd', 'something']}
'id_five': {labels: ['sec:intro'], packages: ['this', 'that', 'hello']}
'id_one': {labels: ['aaa'], packages: {}}
'id_two': {labels: [], packages: {}}
'id_three': {labels: ['bbb', 'ccc'], packages: {}}
'id_four':
labels: []
packages:
baz: [{
caption: '\\longercommandtest{}'
snippet: '\\longercommandtest{$1}'
meta: 'baz-cmd'
score: 50}]
'id_five':
labels: ['sec:intro']
packages:
foo: [
{
caption: '\\bar'
snippet: '\\bar'
meta: 'foo-cmd'
score: 12
}, {
caption: '\\bat[]{}'
snippet: '\\bar[$1]{$2}'
meta: 'foo-cmd'
score: 10
}
]
baz: [
{
caption: '\\longercommandtest{}'
snippet: '\\longercommandtest{$1}'
meta: 'baz-cmd'
score: 50
}
]
}
describe 'getMetaForDoc', ->
@ -127,11 +194,26 @@ describe 'MetaHandler', ->
@fakeDocs =
'doc_one':
lines: [
'\\usepackage[some-options,more=foo]{pkg}'
'\\usepackage[some-options,more=foo]{foo}'
'\\label{aaa}'
]
@fakeMeta = {labels: ['aaa'], packages: ['pkg']}
@fakeMeta =
labels: ['aaa']
packages:
foo: [
{
caption: '\\bar'
snippet: '\\bar'
meta: 'foo-cmd'
score: 12
}, {
caption: '\\bat[]{}'
snippet: '\\bar[$1]{$2}'
meta: 'foo-cmd'
score: 10
}
]
@DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith 1, null
@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith 1, null, @fakeDocs
@MetaHandler.extractMetaFromProjectDocs = sinon.stub().returns @fakeMeta