Sublime Forum

Can the indentation of Sublime Text 3 be improved?

#1

The following code is not indented and Sublime Text 3 seems not able to indent it properly. Compared to JSFiddle that can indent it perfectly… could you please fix this aspect of Sublime Text 3?

function copyTextToClipboard(text) {

// The fallback is needed for Safari / Chrome on iPhone

function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
textArea.style.position = "fixed"; //avoid scrolling to bottom
document.body.appendChild(textArea);
textArea.focus();
textArea.select();

try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}

document.body.removeChild(textArea);
}

if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function() {
console.log('Async: Copying to clipboard was successful!');
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
}
0 Likes

#2

ST indents it fine for me…

0 Likes

#3

How did you indent?

By selecting all text first and then Edit -> Line -> Reindent ?

0 Likes

#4

by not selecting anything and then command palette -> reindent lines, which I believe invokes the same action as from the main menu as you describe.

0 Likes

#5

starting from about line 8, then the text is unindented, with no space in front whatsoever.

Even line 5, it wasn’t indented.

It looks like it is the comment that Sublime Text 3 cannot handle. If the comments are all removed, then it indented fine.

0 Likes

#6

ah I can replicate it if I turn preserveIndent back to true (the default).

0 Likes

#7

If I go to Settings, I don’t see a preserveIndent

Where can that be found… then how come the default is not false…

Where can it be found to change to false.

So an issue 3.5 years ago is still there…

0 Likes

#8

It’s the comment

//avoid scrolling to bottom

at line 8 that confuses the reindent logic. Without it everything works just fine. It’s clearly a bug.

0 Likes

#9

I tried replicating it and clci is right, the Reindent Lines command produces different results if we remove that inline comment, which almost fixes everything. I don’t get proper indentation however with everything inside the main copyTextToClipboard function, it gets ignored.

0 Likes