MCPcopy Create free account
hub / github.com/codecombat/codecombat / parseUserSnippets

Function parseUserSnippets

app/core/aceUtils.js:90–182  ·  view source on GitHub ↗
(source, lang, session)

Source from the content-addressed store, hash-verified

88}
89
90const parseUserSnippets = function (source, lang, session) {
91 const replaceParams = function (str) {
92 let i = 1
93 // eslint-disable-next-line no-useless-escape, no-template-curly-in-string
94 str = str.replace(/(?:[^(,)]+ )?([a-zA-Z-0-9\$\-\u00A2-\uFFFF]+)([^,)]*)/g, '${:$1}') // /(?:[^(,)]+ )?/ part for ignoring the type declaration in cpp/java
95 const reg = /\$\{:/
96 while (reg.test(str)) {
97 str = str.replace(reg, `\${${i}:`)
98 i += 1
99 }
100 return str
101 }
102
103 const makeEntry = function (name, content, importance) {
104 content = content != null ? content : name
105 const entry = {
106 meta: $.i18n.t('keyboard_shortcuts.press_enter', { defaultValue: 'press enter' }),
107 importance: importance != null ? importance : 5,
108 content,
109 name,
110 tabTrigger: name,
111 }
112 return entry
113 }
114
115 const allIdentifiers = {}
116 const newIdentifiers = {}
117
118 const it = new TokenIterator(session, 0, 0)
119 let next = it.getCurrentToken()
120 if (!next) { next = it.stepForward() }
121 while (next) {
122 if (next.type === 'string') {
123 if (!(next.value in newIdentifiers)) {
124 newIdentifiers[next.value] = makeEntry(next.value)
125 } else {
126 newIdentifiers[next.value].importance *= 2
127 }
128 }
129 if (next.type === 'identifier') {
130 if (next.value.length === 1) { // skip single char variables
131 next = it.stepForward()
132 continue
133 }
134 if (!(next.value in allIdentifiers)) {
135 allIdentifiers[next.value] = 5
136 } else {
137 allIdentifiers[next.value] *= 2
138 }
139 }
140 // console.log('deubg next:', next)
141 next = it.stepForward()
142 }
143
144 const lines = source.split('\n')
145 lines.forEach(line => {
146 if (singleLineCommentRegex(lang).test(line)) {
147 return

Callers

nothing calls this directly

Calls 4

makeEntryFunction · 0.85
singleLineCommentRegexFunction · 0.85
identifierRegexFunction · 0.85
replaceParamsFunction · 0.85

Tested by

no test coverage detected