How to merge 2 packages


#1

Hi.

I’d like to know the way to let one package have multiple Functions…
for to merge 2 packages.

  1. to create new memo file: https://github.com/tgfjt/atom-memolist-new
  2. to grep memos : https://github.com/tgfjt/atom-memolist

You know how to do that?


#2

The package that does the grepping (forget which one that is) already does about the right thing: It has a short top-level file that just registers the commands, and then each command basically just forwards the request somewhere else.

So you can create classes for your various views, all in the same package. And the top-level file just exposes two methods, say createMemo and grepMemos, and the top-level file registers those commands during activate:

atom.workspaceView.command "memolist:create", @createMemo
atom.workspaceView.command "memolist:grep", @grepMemos

One problem that you will have is that the “create” thingy will want an option to cancel. To do that, in the initialize function of the “create” view, register a cancel command that’s mapped to the Escape key.


#3

Instead of creating yet another cancel command for your package, register to the core:cancel command, you won’t have to worry about the key binding.


#4

Does this mean that I can specify a new behavior for core:cancel in a specific context? I thought if I did atom.workspaceView.command "core:cancel", @foo, then I redefine the meaning of core:cancel?

Have I misunderstood you? What is going on instead? Any documentation I can read?


#5

Yes, that’s what I mean, sorry if it wasn’t clear, I was just trying to make explicit the fact that you can just redefine the core:cancel command in your views rather than creating a memolist:cancel command.


#6

{Goes and studies the code.}

Oh! I never realized that command was a method on all views! So instead of doing atom.workspaceView.command foo, bar I can do @command foo, bar instead, and it will “localize” the command to this view. Nifty!

{Okay, I admit, it wasn’t studying, it was skimming.}

Thanks a bunch. I got older than a cow, still I learn something now. (Old German proverb.)


#7

So I tried that in my atom-svn package but it doesn’t seem to work. lib/views/file-list.coffee had these two lines:

        atom.workspaceView.command "svn:next", @next
        atom.workspaceView.command "svn:previous", @previous

I tried the following two alternatives:

  • @command "svn:next", @next
  • @command "core:move-down", @next

(Of course, I did it for “previous”, too.) Neither of them worked: the command was not even shown on ctrl-shift-P.


#8

Yes, sorry for the confusion.

To define a new command, one that your package will expose in the command palette (using cmd-shift-p) you have to use atom.workspaceView.command.

To reuse some core commands in your view (typically core:validate or core:cancel) you can/should do it from your view using its command method.

I didn’t dug too deep in this part of atom yet but my understanding is that the commands palette looks at the workspaceView commands, since it’s likely where you’re going to register your custom command.
However, you can always hook to a command from anywhere. This is where the use of css-like selectors for bindings come in handy: Given a scope (a dom element with the keyboard focus), a given command (event) can be dispatched with a specific keystroke.
This is how you can access core:cancel in your view since it’s just a dom event that bubble up and go up to the workspace view if its propagation isn’t stopped in the process.

My knowledge is quite limited in this matter as I didn’t do anything fancy yet with key bindings in the packages I wrote but it should be mostly along these lines. Maybe @leedohm can correct me if I missed something.


#9

As @abe said, I also haven’t done anything fancy with keybindings in any of my packages … but the way he explained it is also the way I understand it to be.


#10

Thanks, @abe. I think this is what happened: I used the view-specific method to bind keys, but the keys didn’t work because keys in general didn’t work. Now that I’ve figured out why keys didn’t work, I can attack this again.

Here is the thread that describes what you need to do to allow keyboard focus:


#11

@kgrossjo
I see. Thank you a lot.
I think You’re be able to cancel “create” with ESC.