Background: I’m trying to write an extension that adds two commands: “Expand selection” and “Shrink selection”. “Expand selection” should work similar to the corresponding functions in other editors (VS Code, Sublime Text) to expand the selection to the parent scope. For example, in the following code console.log('fo|o', bar);
the cursor is positioned between the two o
in foo
. If I execute “Expand selection” the text foo
should be selected, another invocation and 'foo'
should be selected, next one would select 'foo', bar
, and so on. “Shrink selection” works in the opposite direction.
However, currently I’m in the progress of learning the Nova APIs and I’m struggling with how TextEditor.symbolsForSelectedRanges()
and TextEditor.symbolAtPosition()
work.
This is my debug code so far:
exports.activate = function () {
// Do work when the extension is activated
};
exports.deactivate = function () {
// Clean up state before the extension is deactivated
};
nova.commands.register("modify-selection.expandSelection", (editor) => {
editor.symbolsForSelectedRanges().forEach((symbol) => {
console.log(
"symbol",
symbol.type,
symbol.range,
symbol.name,
symbol.displayName
);
});
});
If I invoke the “Expand selection” command on this code, while having type
(line 13) selected, the log shows:
symbol function [294, 294] undefined undefined
That is, it returns the console.log()
function call as the closest symbol, but doesn’t even have the correct end of the range for the symbol.
Another example is selecting extension is activated
(line 2) and invoking “Expand selection”.
symbol function [8, 27] activate activate
In this case the range has a start and end, but I would expect that the deepest displayable symbol (as the function is described in the docs) is actually the comment on line 2, not the function containing the comment.
Is this how the TextEditor.symbolsForSelectedRanges()
and TextEditor.symbolAtPosition()
are supposed to work? Am I missing something in the docs that would allow me to implement the commands I have described above?
Thank you.