Merge pull request #690 from sharelatex/as-autocomplete

Autocomplete
This commit is contained in:
Alasdair Smith 2018-07-05 09:43:37 +01:00 committed by GitHub
commit cf92783a1b
8 changed files with 90 additions and 51 deletions

View file

@ -1,5 +1,5 @@
define [ define [
"./top_hundred_snippets" "./snippets/TopHundredSnippets"
], (topHundred) -> ], (topHundred) ->
class Parser class Parser

View file

@ -1,31 +1,7 @@
define () -> define [
environments = [ 'ide/editor/directives/aceEditor/auto-complete/snippets/Environments'
"abstract", ], (Environments) ->
"align", "align*", staticSnippets = for env in Environments.withoutSnippets
"equation", "equation*",
"gather", "gather*",
"multline", "multline*",
"split",
"verbatim",
"quote",
"center"
]
snippetNames = [
"array",
"figure",
"tabular",
"table",
"list",
"enumerate",
"itemize",
"frame",
"thebibliography"
]
environmentNames = snippetNames.concat(environments)
staticSnippets = for env in environments
{ {
caption: "\\begin{#{env}}..." caption: "\\begin{#{env}}..."
snippet: """ snippet: """
@ -151,7 +127,7 @@ define () ->
result = [] result = []
iterations = 0 iterations = 0
while match = re.exec(text) while match = re.exec(text)
if match[1] not in environmentNames and match[1] != "document" if match[1] not in Environments.all and match[1] != "document"
result.push {name: match[1], whitespace: match[2]} result.push {name: match[1], whitespace: match[2]}
iterations += 1 iterations += 1
if iterations >= 1000 if iterations >= 1000

View file

@ -0,0 +1,29 @@
define () ->
envs = [
"abstract",
"align", "align*",
"equation", "equation*",
"gather", "gather*",
"multline", "multline*",
"split",
"verbatim",
"quote",
"center"
]
envsWithSnippets = [
"array",
"figure",
"tabular",
"table",
"list",
"enumerate",
"itemize",
"frame",
"thebibliography"
]
return {
all: envs.concat(envsWithSnippets)
withoutSnippets: envs
}

View file

@ -53,31 +53,12 @@ define [
lastCommandFragmentIsReqPack lastCommandFragmentIsReqPack
if linesContainMeta or lastCommandFragmentIsMeta if linesContainMeta or lastCommandFragmentIsMeta
@scheduleLoadCurrentDocMetaFromServer() @Metadata.scheduleLoadDocMetaFromServer @$scope.docId
@editor.on "changeSession", (e) => @editor.on "changeSession", (e) =>
e.oldSession.off "change", onChange e.oldSession.off "change", onChange
e.session.on "change", onChange e.session.on "change", onChange
loadDocMetaFromServer: (docId) ->
@Metadata.loadDocMetaFromServer docId
scheduleLoadCurrentDocMetaFromServer: () ->
# De-bounce loading labels with a timeout
currentDocId = @$scope.docId
existingTimeout = @debouncer[currentDocId]
if existingTimeout?
clearTimeout(existingTimeout)
delete @debouncer[currentDocId]
@debouncer[currentDocId] = setTimeout(
() =>
@loadDocMetaFromServer currentDocId
delete @debouncer[currentDocId]
, 1000
, this
)
getAllLabels: () -> getAllLabels: () ->
@Metadata.getAllLabels() @Metadata.getAllLabels()

View file

@ -3,6 +3,7 @@ define [
], (App) -> ], (App) ->
App.factory 'metadata', ($http, ide) -> App.factory 'metadata', ($http, ide) ->
debouncer = {} # DocId => Timeout
state = {documents: {}} state = {documents: {}}
@ -46,4 +47,19 @@ define [
{_csrf: window.csrfToken} {_csrf: window.csrfToken}
) )
metadata.scheduleLoadDocMetaFromServer = (docId) ->
# De-bounce loading labels with a timeout
existingTimeout = debouncer[docId]
if existingTimeout?
clearTimeout(existingTimeout)
delete debouncer[docId]
debouncer[docId] = setTimeout(
() =>
metadata.loadDocMetaFromServer docId
delete debouncer[docId]
, 1000
)
return metadata return metadata

View file

@ -92,3 +92,4 @@
@import "../js/libs/select/select.css"; @import "../js/libs/select/select.css";
@import "vendor/codemirror.css"; @import "vendor/codemirror.css";
@import "vendor/codemirror-dialog.css"; @import "vendor/codemirror-dialog.css";
@import "vendor/codemirror-show-hint.css";

View file

@ -0,0 +1,36 @@
.CodeMirror-hints {
position: absolute;
z-index: 10;
overflow: hidden;
list-style: none;
margin: 0;
padding: 2px;
-webkit-box-shadow: 2px 3px 5px rgba(0,0,0,.2);
-moz-box-shadow: 2px 3px 5px rgba(0,0,0,.2);
box-shadow: 2px 3px 5px rgba(0,0,0,.2);
border-radius: 3px;
border: 1px solid silver;
background: white;
font-size: 90%;
font-family: monospace;
max-height: 20em;
overflow-y: auto;
}
.CodeMirror-hint {
margin: 0;
padding: 0 4px;
border-radius: 2px;
white-space: pre;
color: black;
cursor: pointer;
}
li.CodeMirror-hint-active {
background: #08f;
color: white;
}