Get indentation level/length?

I’m relatively new to Nova and I’m trying to port/rewrite some extensions I miss from VSCode. One of them is the console.log wrap where a given piece of text is selected and it will create a console.log on the next line that has the text selected as a string, and then the selected text (for JavaScript/TypeScript).

And while I have it basically working, I was hoping there was an easy way to determine how far indented in a line is. I assume, if I must, I could getLineRangeForRange, do a regex on the text in that range until I hit the first non-space character, and prepend that text to the beginning of my new line, but I’m hoping there’s an easier way.

Also, this is my first extension, so I welcome feedback: GitHub - craveytrain/nova-console-log-it

Thank you

1 Like

Unfortunately don’t have an answer for you, but will check out the code for sure. Was trying to build something similar a while ago but life got in the way of finishing it so going to be fun to see your approach. :v:

Using getLineRangeForRange seems sensible to work out the leading whitespace. A regex could work, maybe like /^(\s+)/, to match the initial whitespace then use that captured group as a prefix when inserting lines into the document. You might be able to construct the the regex based on tabText to only match whitespace in the current document’s style?

You can also access softTabs, tabLength & tabText properties on the TextEditor to determine the current indentation style, if useful (Api docs).