How to get indent level of line in userscript

In a userscript, I’m trying to figure out the best way to get the indent level of any line in a file, i.e., how many times the line has been indented to the right. So far I have:

const SC_FOLDLEVELBASE = 0x400;    //  Set in contrib/scintilla/include/Scintilla.h and Scintilla.iface
function indent_level( line_number ) {
    return ( require( "ko/editor" ).scimoz().getFoldLevel( line_number ) % SC_FOLDLEVELBASE ) - 1;

I’m using the samples in C:\Users\[user]\AppData\Local\ActiveState\KomodoIDE\10.2\samples\ for testing. The code works for JavaScript and PHP files, but not Python. For example, here are some lines from php_sample.php:

class foo {
    var $a;
    var $b;
    function display() {
        echo "This is class foo\n";
        echo "a = ".$this->a."\n";
        echo "b = ".$this->b."\n";

So the indent levels of those seven lines are 0, 1, 1, 1, 2, 2, 2 – and that’s what my code returns. But for a Python file like

def printStrings(mylist):
    '''This function will print all the items in list that are a string type'''
    import types
    for element in mylist:
        if type(element) == types.StringType:
            print "element %s is a string" % element

The code returns -1, 3, 3, 3, 7, 11, 3 – which has the pattern of being 1 less than the number of spaces, not indents.

Any ideas what is going on or how it can be done better?

@mjross I think you are searching for the .getLineIndentation(line) function.
This will return the indentation level of the line.

Created a small userscript to test the results:

var scimoz =require( "ko/editor" ).scimoz();
var line =+ ko.interpolate.interpolateString('%(ask:line-number)');

// Line numbers are 0 indexed, so -1 will correct this (visual)

// Figure out if to use tab width
var indentation = scimoz.useTabs ? scimoz.tabWidth : scimoz.indent;

// This will show up in you're console
console.log('Line is indented by: ' + (scimoz.getLineIndentation(line) / indentation) + ' indents');

Updated the userscript, noticed you where searching for the indent level and not only the amount of indent characters.
If you divide the line indentation with scimoz.indent( the size of indentation in terms of the width of a space ):

scimoz.getLineIndentation(line) / scimoz.indent; // will return indent level

This will return the current indent level.

Be careful, scimoz.indent can be 0, in which case scimoz.tabWidth has the correct value.

Thanks to both of you for your responses! I will try out that code, and also be careful regarding scimoz.indent possibly being zero. Under what circumstances would that happen?

scimoz.indent is zero in the cases we haven’t accounted for it to be zero. Ideally it would never be zero, but over the years as we’ve added stuff, it being zero creeps in every now and then. There’s no way to say for sure when it would be. Sorry.

No worries. Thanks!

@mitchell updated the example to deal with tab with, better this way?

That certainly allows for more flexibility. But I’m wondering if it wouldn’t be better to always use scimoz.tabWidth instead of scimoz.indent? Based on what I have heard so far, it sounds like scimoz.indent cannot always be relied upon. Or do we know that scimoz.indent ends up being erroneously zero only in cases when scimoz.useTabs is true (and thus in your code we would always use scimoz.tabWidth in those cases)?