New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting for using spaces instead of tabs #988
Comments
Rebind the |
Do you mind having an option like function(cm){ cm.replaceSelection(Array(cm.getOption('tabSize')).join(" "); } Also to be honest I expected |
No. I went down the road of building in N indentation models in CM version 1. It was a mess and still didn't cover everything people wanted. Just use the building blocks provided to do what you want to do. |
Issues codemirror#988, codemirror#742, codemirror#612, etc. show that CodeMirror users tend to: 1. Not find how to make the Tab key insert spaces instead of a tab, 2. Miss how to use extraKeys to easily solve the issue. Adding an example helps them find a solution immediately.
When using the "cm.replaceSelection"-solution, it is no longer possible to indent code by marking multiple lines and pressing "Tab". Is there a workaround or code sample that shows how to keep that (very useful) functionality, while still enabling insertion to be spaces instead of a tab? |
@elygre Sure there is! A bit of fiddling with the API: function betterTab(cm) {
if (cm.somethingSelected()) {
cm.indentSelection("add");
} else {
cm.replaceSelection(cm.getOption("indentWithTabs")? "\t":
Array(cm.getOption("indentUnit") + 1).join(" "), "end", "+input");
}
}
CodeMirror.fromTextArea(document.getElementById("the-editor"), {
extraKeys: { Tab: betterTab }
}); |
Brilliant. This should probably go in the documentation, instead of the version that doesn't handle this scenario. Thanks. |
Thumbs up for including that in the docs. I`ve lost some time looking for the way to do it right. A simpler, more user friendly, method wouldn't hurt either. |
You're a lifesaver, sir. Thank you! |
Spaces are mandated at our organization. Tabs are not consistent enough across editors and terminals and we got tired of having to reconfigure every editor and terminal in order to display source correctly. It's pretty annoying that CodeMirror doesn't have a simple way to do this. The method in the docs is incomplete, and @espadrine provides a partial solution above. |
@anodos I do not believe there is a "bug" in the above solutions, it is just that they implement something that is not what you want. Since you didn't bother to explain what exactly it is that you want, I can't really comment on how to do it. Maybe binding to the "indentAuto" command is what you are trying to do? |
I did bother to explain exactly what I want, so I will quote myself, "If you are in column 2 then pressing TAB should add enough spaces to take you to column 4, but instead [with the examples given so far] TAB always inserts 4 spaces no matter what column you are on." |
|
I think what we must have here is a failure to communicate. You are sensing a tone of entitlement, so I can see you are misunderstanding me, since I meant no such tone. It is also very possible at this point that you won't try to understand what I am saying. But I will give it one last go. |
I understand what you wanted (the previous comment made it clear). I'm just challenging the notion that this is an obvious default/built-in behavior. I've had at least ten different whitespace/tab key/indentation set-ups being pushed onto the system as 'the obviously right way', and at this point I've given up providing something that works for everyone, and my response to such things is "use the API, customize it to your heart's content" |
I encountered this issue quite a while ago, and found / was told about the customization I required (see further above). Now, if this is a frequent enough request that people push many different variations, perhaps a good solution could be an easier accessible documentation of those? For example, on http://codemirror.net/doc/manual.html, a new heading below "Keymaps" called "Tabs" or somethings, with a listing of various ways of doing this? If the heading was created, I could surely provide text for at least the one we're currently using, including pros & cons and the actual code. We might perhaps even provide examples for each of them various ways. |
Hello. Every time I use Code Mirro, I had to set this:
Could we have an option to do that ? I know you don't want, but really, it's not easy for new comers (like me :p ). More over this feature is really needed when editing YAML files. If you still don't want, Can we add this snippet to the documentation ? |
This snippet is in the docs (http://codemirror.net/doc/manual.html#keymaps). Key bindings aren't options in CodeMirror. Are the CodeMirror-using sites you use not under your control, that you can't simply add this once and for all? A bookmarklet might also be a solution. |
Oups, The last time I used code mirror (~1 year ago), the snippet was not in the docs ;) Thanks. |
If the requirement is to emulate the behaviour of Visual Studio (with Tabs: Insert Spaces) or emacs with "indent-tabs-mode nil", or countless other editors, then the idea is to make it look like a tab has been inserted, so the cursor moves to the next tab stop. Therefore the count has to be reduced by the modulo of the current cursor position with the whole count. The example on this page (and in the documentation) doesn't do that. You would need something like this:
|
(You don't want to randomly reach into undocumented internals like this -- use |
Thanks - I edited my previous comment accordingly. Also added the check for somethingSelected() to avoid interfering with the built-in (very standard) behaviour for indenting a block of selected text with Tab, or un-indenting it with Shift+Tab. Ironically, that built-in handling of a selection does a fine job of inserting spaces! It seems strange that in the default configuration, the end user will get a mixture of tabs and spaces depending on whether they hit the tab key with or without a range selected. |
Um... This is my tab method: cm.addKeyMap({
Tab: (cm) => {
if (cm.somethingSelected()) {
cm.indentSelection("add");
return;
}
if (this.config.indentWithTabs)
cm.replaceSelection("\t", "end", "+input");
else
cm.execCommand("insertSoftTab");
},
"Shift-Tab": (cm) => {
cm.indentSelection("subtract");
}
}); When |
The version you are using probably does not have the |
…ion. Before this, CodeMirror was storing literal tabs. They align in the IDE because it has tab size set to 2, but are inconsistent when saved to disk or viewed in another editor. The issue is discussed and my solution comes from this CodeMirror thread: codemirror/codemirror5#988
0fd1dab replaced tabs with four spaces on compilation but the tabs remaind in the code. This commit uses codemirror/codemirror5#988 (comment) to insert four spaces by pressing tab and preserves the features to indent a code block and to un-indent.
I'm having a little trouble in a use case I want to support. I want to let the users paste their code or upload the code from a file. I have been thinking that is not very efficient to replace the tabs in the code string whenever it changes. I would like to do this without changing the source code of codemirror. any ideas? |
You can use the |
In case this is useful for anyone else, my favorite solution: extraKeys: {
Tab: (cm) => cm.execCommand("indentMore"),
"Shift-Tab": (cm) => cm.execCommand("indentLess"),
}, It couldn't be simpler, and it has the following behavior, which I think is 👌:
Now the only thing left to figure out is how to delete 2 spaces at a time instead of just one… |
@chriscalo Indenting the current line with no selection would probably annoy a lot of people. It’s very common to line up “table-like” declarations by hitting the tab key at end or middle of the line. |
issue in codemirror prevents working properly for now: codemirror/codemirror5#988 Refs https://gitlab.ilscipio.com/scipio-dev/dev/scipioce-dev/issues/560
If having the setting: indentWithTabs : false,
smartIndent : true, Then the following setting might be good for a better experience of using the tab key. extraKeys: {
Tab: (cm) => {
if (cm.getMode().name === 'null') {
cm.execCommand('insertTab');
} else {
if (cm.somethingSelected()) {
cm.execCommand('indentMore');
} else {
cm.execCommand('insertSoftTab');
}
}
},
'Shift-Tab': (cm) => cm.execCommand('indentLess')
} |
I love your solution |
Your way of getting the indentation didn't work for me, let cursorPos = cursorsPos[cursorIndex];
let lineContent = cm.doc.getLine(cursorPos.line);
let indentation = lineContent.match(/^\s+/)?.[0].length ?? 0; |
The solution by @windy here works great, but has one minor bug. If you indent the a blank line, then move the cursor to the beginning of the line and try to backspace, nothing happens. That's because in the conditional: if (!(indentation !== 0 &&
cursorPos.ch <= indentation &&
cursorPos.ch % indentUnit === 0)) {
shouldDelChar = true;
} We don't meet the conditional because We found several situations where backspace didn't work at the beginning of the line. if (!(indentation !== 0 &&
cursorPos.ch > 0 &&
cursorPos.ch <= indentation &&
cursorPos.ch % indentUnit === 0)) {
shouldDelChar = true;
} Making this change seems to fix the issue. Note I added |
Our YAML parser does not like tabs as indentdation marker. Therefore CodeMirror is now configured to ident with 2 spaces per identation unit. Futhermore the Tab and Shift Tab behaviour has been overwritten to use spaces "inteligently" instead of tabs. Information about this procedure was taken from here: codemirror/codemirror5#988 (comment)
Update CodeMirror options as suggested by codemirror/codemirror5#988 (comment) in order to indent code using spaces instead of tabs. Tested by running playground (`./bin/crystal play`) and updating some code: https://user-images.githubusercontent.com/153842/178105140-4aad0486-a937-4686-8904-5e9c0a87145b.mov
I may be missing something but I'm not able to detect a way to configure codemirror such that it would use spaces for indentation when a tabs button is used. Maybe there is a way to do it via custom keys but even so it's common enough case (since all editors have such option) to have a dedicated option for that.
The text was updated successfully, but these errors were encountered: