Hitting a wall getting started on first extension

I’ve never developed an extension before, so much of this is new to me; and I’m finding the docs a touch lacking. I’ve been trying to learn by looking at the repos of other extension projects, and I’ve at least been able to get a ‘Hello world’ printed to the extension console. That’s where I’m currently stuck however.

Within exports.activate = function..., I want to test when there is text change in the editor, so in the activate function, I have:

nova.textEditor.onDidChange((editor) => console.log(editor));

But that is throwing an undefined error on the nova object. It appears to me from the docs and other repos that nova is available globally and not something I need to import directly in the project file to call. Can anyone point me in the right direction?

While nova should be available anywhere, it does not have a specific textEditor element.

Since each “window” can have multiple text files open, that window is considered a Workspace. With that Workspace, you could try something like nova.workspace.activeTextEditor which will only return the current editor. But, if you really want to interact with assigning a callback to the onDidChange, you may need to do something else:

const MinifierService = require("./minifier.js");

exports.activate = function() {
	const Minifier = new MinifierService();

	nova.workspace.onDidAddTextEditor((editor) => {
		return editor.onWillSave(Minifier.minifyOnSave.bind(Minifier));
	});
}

exports.deactivate = function() {
	// Clean up state before the extension is deactivated
}

So when the extension activates, it will that assign an event listener that triggers whenever a text editor is opened in the workspace. The onDidAddTextEditor will pass editor which is the TextEditor object you are looking for. Now, in that listener, I have it assign onWillSave to call the Minifier to do it’s stuff. That’s where you may want to do something with the onDidChange there instead.

I agree, learning to make extensions is sometimes easier to take a look at other extensions than the current documentation. :slight_smile:

In case you didn’t know, you can also go into Extensions → Extensions Library… and then right-click on an extension and Show in Finder. And then open that folder in Nova, and poke around at the code.

Let me know if that helps!

Ok, that makes sense! Thanks very much for the direction!

1 Like