How to best get classes & functions

Am looking to generate a list of all class/function names in the current document, which, when clicked on will jump to that line in the file.

Currently using cm.eachLine and getting handle, then performing an indexOf(‘class’) and indexOf(‘function’) on ‘text’ returned in the handle. This works OK, but of course it’s not perfect as it’ll pick up false positives for those words appearing in places where it’s not declaring a class or function.

Is there a more reliable way to only identify lines that are truly initialising a class/function? Ideally I’d like to also pick up the arguments on them also.

I could potentially look at the ‘styles’ array in the handle returned and if it contains a ‘def’ value, it’s possible that the line containing class/function is defining something. This seems like a questionable way to cover this though and wonder if there’s a better approach available somehow?

Run a real parser over the document. That’ll get you much more accurate results in an easier way. For some modes, you might be able to get enough info out of the tokens, but code highlighting is usually not 100% precise.

OK thanks very much Marijn. :slight_smile: