Sie sind auf Seite 1von 3

Search…

Home

PUBLIC
CodeMirror AutoComplete Custom List
Stack OverflowAsk Question

Tags asked 4 years, 9 months ago

Users viewed 13,390 times


8
Jobs active 1 year, 9 months ago

Log In Sign Up
TEAMS
Looking for a job?
Create Team 2
I am having a bit of difficultly re the auto complete function in code mirror. What I am trying to do is Senior/Lead .Net Developer (full stack)
two things (both which I am struggling with): Simplicity First Solutions, LLC Pune, India
c# .net
1) I want to enable auto complete for both HTML and JavaScript . Currently I can only get one
working at a time using e.g.:
Family-Friendly team seeks Junior/Mid-
CodeMirror.commands.autocomplete = function (cm) { Level Developer (JavaScript NodeJS
CodeMirror.showHint(cm, CodeMirror.hint.html); React)
}; Spidergap No office location
£20K - £40K REMOTE
How can I add the CodeMirror.hint.javascript to the list from the HTML one? javascript html

2) (Kind of more important) -- How can I add custom variables to the list of hints from HTML
Short Term Contract Xamarin Hybrid
which area retrieved from an ajax call..... Shell App Developer
PalabraApps No office location
i.e. I want to have the drop down show up with the current list of data from the html hints, but then
$60K - $96K REMOTE
add custom entries such as ##SomeCode1## and ##SomeCode2##
xamarin asp.net-mvc-4
I have two problems here. First when I try to hard code the values in the 'html-hint.js' file the values
all get appended with < ... which is not what I want. Senior C# Web Developer (work from
home!)
The second problem, kind of, is that I believe I have to write a new 'html-hint.js' file correct? I mean Powerfront Pty Ltd No office location
there is no way to pass anything in the 'options' parameter in the CodeMirror.hint.html above is $12K - $36K REMOTE
there, to essentially merge two list. c# .net

I guest one and two are kind of the same come to think of it... Merging two lists of values for auto
complete together.
Linked
I am guessing there is nothing already in the framework and I have to write a custom hint file,
correct? 4 Writing a custom mode for CodeMirror, for
use in Brackets
Any pointers would be appreciated. Sample code would be awesome. 7 CodeMirror custom hint list not working
properly
codemirror
2 Live autocomplete in Codemirror
share improve this question
0 ng2-codemirror - How to add custom
edited Jul 8 '16 at 14:53
keywords to autocomplete (hint)
WMios
6,255 9 33 57 0 Overwriting a Javascript function in
Typescript
asked Oct 8 '13 at 9:51
Robin Rieger Related
849 1 11 29

2 Codemirror not compatible with Jquery


Autocomplete

2 Answers active oldest votes 2 JSException while loading a file in a


codemirror based editor using java using
setvalue

1 Codemirror autocomplete addon event


binding

17 2 CodeMirror with Tern - Custom “Types” with


Javascript Intellisense

0 CodeMirror OnSelect event not working

0 Using both closetag.js & closebrackets.js


in Codemirror on same line
If you do not specify a hint function, the show-hint addon will take the hint helper function defined for
the local mode at which the completion happens, so that will be CodeMirror.hint.javascript in 0 How to set a hidden values in CodeMirror
JavaScript code, and CodeMirror.hint.html in HTML. editor?

1 CodeMirror - Putting Code in Element Tags


If you need to add your own completion logic, you can replace (or wrap) these functions by simply
overwriting them with your own code. 0 CodeMirror issue: Error parsing forward
channel Error: Invalid maps at
Here is a crude example hack that always adds "bozo" to JavaScript completions: transformData

var orig = CodeMirror.hint.javascript; 0 How to define multiple list for autocomplete


CodeMirror.hint.javascript = function(cm) { using codemirror?
var inner = orig(cm) || {from: cm.getCursor(), to: cm.getCursor(), list: []};
inner.list.push("bozo"); Hot Network Questions
return inner;
};
Mounted tv too high, OK to use same studs to
remount?
share improve this answer
What differentiates "Gods" from other entities in
edited Jul 8 '16 at 14:55 the Forgotten Realm cosmology?
WMios
Why integrate over accumulation?
6,255 9 33 57
What is the official term for a gas / petrol station
answered Oct 9 '13 at 10:47 for planes?
Marijn Man-portable anti-satellite weapon
6,016 1 17 23
Can a body be in equilibrium when only one force
acts on the body?
1
Which strange fruit is this?
Very helpful. Maybe you can extend your answer how to make real autocompletion like when I'm entered
"bo" only "bozo" will be suggested and not f.e. "foobar" and that it does not add "bozo" behind "bo", but How to deal with low-probability high-impact
risks?
replaces "bo" with "bozo". Thanks :) – MonkeyMonkey Jan 29 '14 at 15:45
Code to track execution time for each function in a
class

+1 for bozo you jabroni! – Dänu Sep 19 '14 at 14:02 How can we find a dog to "test drive"?

1 Is life insurance a financially sound safety net?

Marijn, the code above doesn't work as expected. The word 'bozo' is indeed added to the list, but as I type 'b- How to avoid the language barrier when there is
o-z...' it is not selected. On the contrary, if my page has a function named 'monkey,' as I type 'm-o-n...' that no time to learn the other language?
name is indeed selected. Also, the word 'bozo' is always displayed, not just when the user types a similar Anagrams: From Food to Food
word. This is not the expected behavior for autocompletion. Are there any other ways to accomplish this? –
shmim May 20 '16 at 18:54 How do I bend a line onto a circle?

1 "Long-standing conjectures in analysis ... often


turn out to be false"
The hint function is responsible for filtering. If it just unconditionally adds a word, as in my example, no
Why did ones' complement decline in popularity?
filtering happens. But you can add your own logic there if you want. – Marijn May 21 '16 at 8:14
Does a Wild Magic Surge trigger at the beginning
or the end of the casting of a spell?

Add IF Condition to run another class

What is the difference between a loss function


and an error function?

"What is the weather today?" or "How is the


5 weather today?"

The role of the supernatural in hard science


fiction
Thanks to @Marjin for brief explanation, but since it doesn't cover filtering and a lot of people needs Why is `sort < "$f1" ` preferred over `sort -- "$f1"`,
it, here's what I've done in mongoclient by following Marjin's answer. And partially took help from and why is this preferred over `sort "$f1"`?
here Howdy! I'm the sheriff of code golf!

p.s. Don't forget to change hint with yours, since I'm using javascript I've changed javascript hint. Why do two identical lists have a different
memory footprint?
CodeMirror.hint.javascript = function (editor) {
var list = Session.get(Template.strSessionDistinctFields) || []; question feed
var cursor = editor.getCursor();
var currentLine = editor.getLine(cursor.line);
var start = cursor.ch;
var end = start;
while (end < currentLine.length && /[\w$]+/.test(currentLine.charAt(end))) ++end;
while (start && /[\w$]+/.test(currentLine.charAt(start ‐ 1))) ‐‐start;
var curWord = start != end && currentLine.slice(start, end);
var regex = new RegExp('^' + curWord, 'i');
var result = {
list: (!curWord ? list : list.filter(function (item) {
return item.match(regex);
})).sort(),
from: CodeMirror.Pos(cursor.line, start),
to: CodeMirror.Pos(cursor.line, end)
};

return result;
};

share improve this answer

answered Oct 11 '16 at 8:21


Sercan Ozdemir
3,237 1 24 52

Is it possible to add custom hint list only to a certain CodeMirror instance? – peterremec Dec 5 '16 at 11:39

ofc, in that case you need to create your own mode for codemirror, stackoverflow.com/questions/24433552/
… – Sercan Ozdemir Dec 5 '16 at 11:51

I'm not sure if we understand each other. I have two CodeMirror "sql mode" instances on a page with
autocomplete function on. I need to add some custom keywords in addition to a built-in sql keywords. I don't
understand why should I create a new mode. Besides that I'm looking for a way to dynamically add / remove
keywords from that list – peterremec Dec 5 '16 at 12:46

No problem, thank you for your response. – peterremec Dec 5 '16 at 13:43

no you need to escape per special charachter when they come from user input for example for the @ sign
you need to write it like \@ before giving into RegExp constructor – Sercan Ozdemir Oct 17 '17 at 7:17

show 8 more comments

Your Answer

Sign up or log in Post as a guest

Sign up using Google Name

Sign up using Facebook Email


required, but never shown
Sign up using Email and Password

Post Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie
policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged codemirror or ask your own question.

STACK OVERFLOW

Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile
Disable Responsiveness

PRODUCTS

Teams
Talent
Engagement
Enterprise

COMPANY

About
Press
Work Here
Legal
Privacy Policy
Contact Us

STACK EXCHANGE
NETWORK

Technology
Life / Arts
Culture / Recreation
Science
Other

Blog

Facebook

Twitter

LinkedIn
site design / logo © 2018 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2018.7.26.31180

Das könnte Ihnen auch gefallen