<commit_before># Creating Your First Package

Let's take a look at creating your first package.

To get started, hit `cmd-p`, and start typing "Package Generator" to generate
a package. Once you select the "Generate Package" command, it'll ask you for a
name for your new package. Let's call ours _changer_.

Atom will pop open a new window, showing the _changer_ with a default set of
folders and files created for us. Hit `cmd-p` and start typing "Changer." You'll
see a new `Changer:Toggle` command which, if selected, pops up a greeting. So far,
so good!

In order to demonstrate the capabilities of Atom and its API, our Changer plugin
is going to do two things:

1. It'll show only modified files in the file tree
2. It'll append a new pane to the editor with some information about the modified
files

Let's get started!

## Changing Keybindings and Commands

Since Changer is primarily concerned with the file tree, let's write a
key binding that works only when the tree is focused. Instead of using the
default `toggle`, our keybinding executes a new command called `magic`.

_keymaps/changer.cson_ should change to look like this:

```coffeescript
'.tree-view':
  'ctrl-V': 'changer:magic'
```

Notice that the keybinding is called `ctrl-V` &mdash; that's actually `ctrl-shift-v`.
You can use capital letters to denote using `shift` for your binding.

`.tree-view` represents the parent container for the tree view.
Keybindings only work within the context of where they're entered. In this case,
hitting `ctrl-V` anywhere other than tree won't do anything. Obviously, you can
bind to any part of the editor using element, id, or class names. For example,
you can map to `body` if you want to scope to anywhere in Atom, or just `.editor`
for the editor portion.

To bind keybindings to a command, we'll need to do a bit of association in our
CoffeeScript code using the `rootView.command` method. This method takes a command
name and executes a callback function. Open up _lib/changer-view.coffee_, and
change `rootView.command "changer:toggle" to look like this:

```coffeescript
rootView.command "changer:magic", => @magic()
```

It's common practice to namespace your commands with your package name, separated
with a colon (`:`).

Every time you reload the Atom editor, changes to your package code will be reevaluated,
just as if you were writing a script for the browser. Reload the editor, click on
the tree, hit your keybinding, and...nothing happens! What the heck?!

Open up the _package.json_ file, and find the property called `activationEvents`.
Basically, this key tells Atom to not load a package until it hears a certain event.
Change the event to `changer:magic` and reload the editor:

```json
"activationEvents": ["changer:toggle"]
```

Hitting the key binding on the tree now works!

## Working with Styles

The next step is to hide elements in the tree that aren't modified. To do that,
we'll first try and get a list of files that have not changed.

All packages are able to use jQuery in their code. In fact, there's [a list of
the bundled libraries Atom provides by default][bundled-libs].

We bring in jQuery by requiring the `atom` package and binding it to the `$` variable:

```coffeescript
{$} = require 'atom'
```

Now, we can define the `magic` method to query the tree to get us a list of every
file that _wasn't_ modified:

```coffeescript
magic: ->
  $('ol.entries li').each (i, el) ->
    if !$(el).hasClass("status-modified")
      console.log el
```

You can access the dev console by hitting `alt-cmd-i`. Here, you'll see all the
statements from `console` calls. When we execute the `changer:magic` command, the
browser console lists items that are not being modified (_i.e._, those without the
`status-modified` class). Let's add a class to each of these elements called `hide-me`:

```coffeescript
magic: ->
  $('ol.entries li').each (i, el) ->
    if !$(el).hasClass("status-modified")
      $(el).addClass("hide-me")
```

With our newly added class, we can manipulate the visibility of the elements
with a simple stylesheet. Open up _changer.css_ in the _stylesheets_ directory,
and add a single entry:

```css
ol.entries .hide-me {
  display: none;
}
```

Refresh Atom, and run the `changer` command. You'll see all the non-changed
files disappear from the tree. There are a number of ways you can get the list
back; let's just naively iterate over the same elements and remove the class:

```coffeescript
magic: ->
  $('ol.entries li').each (i, el) ->
    if !$(el).hasClass("status-modified")
      if !$(el).hasClass("hide-me")
        $(el).addClass("hide-me")
      else
        $(el).removeClass("hide-me")
```

## Creating a New Panel

The next goal of this package is to append a panel to the Atom editor that lists
some information about the modified files.

To do that, we're going to first open up [the style guide][style-guide]. The Style
Guide lists every type of UI element that can be created by an Atom package. Aside
from helping you avoid writing fresh code from scratch, it ensures that packages
have the same look and feel no matter how they're built.

Every package that extends from the `View` class can provide an optional class
method called `content`. The `content` method constructs the DOM that your
package uses as its UI. The principals of `content` are built entirely on
[SpacePen][space-pen], which we'll touch upon only briefly here.

Our display will simply be an unordered list of the file names, and their
modified times. A basic Panel element will work well for us. Let's start by
carving out a `div` to hold the filenames:

```coffeescript
@content: ->
  @div class: "panel", =>
    @div class: "panel-heading", "Modified Files"
    @div class: "panel-body padded", outlet: 'modifiedFilesContainer', =>
      @ul class: 'modified-files-list', outlet: 'modifiedFilesList', =>
        @li 'Modified File Test'
        @li 'Modified File Test'
```

You can add any HTML attribute you like. `outlet` names the variable your
package can use to manipulate the element directly. The fat pipe (`=>`)
indicates that the next DOM set are nested children.

We'll add one more line to the end of the `magic` method to make this pane appear:

```coffeescript<commit_msg>Indicating the color in the stylesheet is important<commit_after># Creating Your First Package

Let's take a look at creating your first package.

To get started, hit `cmd-p`, and start typing "Package Generator" to generate
a package. Once you select the "Generate Package" command, it'll ask you for a
name for your new package. Let's call ours _changer_.

Atom will pop open a new window, showing the _changer_ with a default set of
folders and files created for us. Hit `cmd-p` and start typing "Changer." You'll
see a new `Changer:Toggle` command which, if selected, pops up a greeting. So far,
so good!

In order to demonstrate the capabilities of Atom and its API, our Changer plugin
is going to do two things:

1. It'll show only modified files in the file tree
2. It'll append a new pane to the editor with some information about the modified
files

Let's get started!

## Changing Keybindings and Commands

Since Changer is primarily concerned with the file tree, let's write a
key binding that works only when the tree is focused. Instead of using the
default `toggle`, our keybinding executes a new command called `magic`.

_keymaps/changer.cson_ should change to look like this:

```coffeescript
'.tree-view':
  'ctrl-V': 'changer:magic'
```

Notice that the keybinding is called `ctrl-V` &mdash; that's actually `ctrl-shift-v`.
You can use capital letters to denote using `shift` for your binding.

`.tree-view` represents the parent container for the tree view.
Keybindings only work within the context of where they're entered. In this case,
hitting `ctrl-V` anywhere other than tree won't do anything. Obviously, you can
bind to any part of the editor using element, id, or class names. For example,
you can map to `body` if you want to scope to anywhere in Atom, or just `.editor`
for the editor portion.

To bind keybindings to a command, we'll need to do a bit of association in our
CoffeeScript code using the `rootView.command` method. This method takes a command
name and executes a callback function. Open up _lib/changer-view.coffee_, and
change `rootView.command "changer:toggle" to look like this:

```coffeescript
rootView.command "changer:magic", => @magic()
```

It's common practice to namespace your commands with your package name, separated
with a colon (`:`).

Every time you reload the Atom editor, changes to your package code will be reevaluated,
just as if you were writing a script for the browser. Reload the editor, click on
the tree, hit your keybinding, and...nothing happens! What the heck?!

Open up the _package.json_ file, and find the property called `activationEvents`.
Basically, this key tells Atom to not load a package until it hears a certain event.
Change the event to `changer:magic` and reload the editor:

```json
"activationEvents": ["changer:toggle"]
```

Hitting the key binding on the tree now works!

## Working with Styles

The next step is to hide elements in the tree that aren't modified. To do that,
we'll first try and get a list of files that have not changed.

All packages are able to use jQuery in their code. In fact, there's [a list of
the bundled libraries Atom provides by default][bundled-libs].

We bring in jQuery by requiring the `atom` package and binding it to the `$` variable:

```coffeescript
{$} = require 'atom'
```

Now, we can define the `magic` method to query the tree to get us a list of every
file that _wasn't_ modified:

```coffeescript
magic: ->
  $('ol.entries li').each (i, el) ->
    if !$(el).hasClass("status-modified")
      console.log el
```

You can access the dev console by hitting `alt-cmd-i`. Here, you'll see all the
statements from `console` calls. When we execute the `changer:magic` command, the
browser console lists items that are not being modified (_i.e._, those without the
`status-modified` class). Let's add a class to each of these elements called `hide-me`:

```coffeescript
magic: ->
  $('ol.entries li').each (i, el) ->
    if !$(el).hasClass("status-modified")
      $(el).addClass("hide-me")
```

With our newly added class, we can manipulate the visibility of the elements
with a simple stylesheet. Open up _changer.css_ in the _stylesheets_ directory,
and add a single entry:

```css
ol.entries .hide-me {
  display: none;
}
```

Refresh Atom, and run the `changer` command. You'll see all the non-changed
files disappear from the tree. There are a number of ways you can get the list
back; let's just naively iterate over the same elements and remove the class:

```coffeescript
magic: ->
  $('ol.entries li').each (i, el) ->
    if !$(el).hasClass("status-modified")
      if !$(el).hasClass("hide-me")
        $(el).addClass("hide-me")
      else
        $(el).removeClass("hide-me")
```

## Creating a New Panel

The next goal of this package is to append a panel to the Atom editor that lists
some information about the modified files.

To do that, we're going to first open up [the style guide][style-guide]. The Style
Guide lists every type of UI element that can be created by an Atom package. Aside
from helping you avoid writing fresh code from scratch, it ensures that packages
have the same look and feel no matter how they're built.

Every package that extends from the `View` class can provide an optional class
method called `content`. The `content` method constructs the DOM that your
package uses as its UI. The principals of `content` are built entirely on
[SpacePen][space-pen], which we'll touch upon only briefly here.

Our display will simply be an unordered list of the file names, and their
modified times. A basic Panel element will work well for us. Let's start by
carving out a `div` to hold the filenames:

```coffeescript
@content: ->
  @div class: "panel", =>
    @div class: "panel-heading", "Modified Files"
    @div class: "panel-body padded", outlet: 'modifiedFilesContainer', =>
      @ul class: 'modified-files-list', outlet: 'modifiedFilesList', =>
        @li 'Modified File Test'
        @li 'Modified File Test'
```

You can add any HTML attribute you like. `outlet` names the variable your
package can use to manipulate the element directly. The fat pipe (`=>`)
indicates that the next DOM set are nested children.

Once again, you can style `li` elements using your stylesheets. Let's test that
out by adding these lines to the _changer.css_ file:

```css
ul.modified-files-list {
  color: white;
}
```

We'll add one more line to the end of the `magic` method to make this pane appear:

```coffeescript
{'fraction_non_alphanumeric': 0.08582881395897195, 'fraction_numerical': 0.00031439125992297417, 'mean_word_length': 4.147249190938512, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7429963', 'n_tokens_mistral': 3692, 'n_tokens_neox': 3505, 'n_words': 1898}