Posted on

Using SourceMate 3.0 to create code bookmarks

Something I’ve been wanting for a while was a slick way to have “bookmarks” in my code to help me jump quickly to certain places in the source files. It’s not uncommon for me to have a long list of files open in Flash Builder. Some files are unnecessarily large, with far more functions than should be there. These problems are something we are all guilty of at one point or another.

I recently installed the trial version of SourceMate 3.0 from Element River. It’s got a ton of productivity enhancements for Flash Builder 4.5+. It doesn’t have a bookmarks feature, but what does it does have is task tags. I really like this feature in general but I found it can be used to create bookmarks.

  1. I created a new tag “BKMK” in the SourceMate tags preferences (Preferences > SourceMate > TaskTags).
    Add “BKMK” task tag

    Now, when I add a comment that starts with BKMK, it will show up in the tasks view.
    // BKMK Place where I go to often

  2. Then, using the menu for the tasks view, I showed a new view and named it Bookmarks.

    Create a new Tasks view
  3. I chose Configure Contents… from the menu and created a new configuration called Bookmarks. For that configuration, I added Text: contains BKMK.

    Create new Bookmarks configuration
  4. Leaving that dialog and returning to the Flash Builder window, I chose Show > Bookmarks for that tasks view and viola, this Bookmarks view will always show the list of BKMK items I’ve placed inline in my code.

    Show Bookmarks configuration in the new view

This requires adding lines to the source code, so unlike breakpoints, it requires modification but can still be a pretty good workaround for the time being.

Posted on

ArrayCollection contains() uses your sort!

Here’s another gotcha that took me a while to realize what was wrong.

I was using an ArrayCollection to hold a collection of items. I had a custom sort applied since the items were typed objects that needed to be sorted according to certain, out-of-the-ordinary fields. In the sort method, I was comparing values and returning either -1 or 1.

return itemA > itemB ? -1 : 1;

From the docs for a compareFunction:

The method used to compare items when sorting. If you specify this property, Flex ignores any compareFunction properties that you specify in the ISortField objects that you use in this class.

The compare function must have the following signature:

         function [name](a:Object, b:Object, fields:Array = null):int

This function must return the following value:

  • -1, if the Object a should appear before the Object b in the sorted sequence
  • 0, if the Object a equals the Object b
  • 1, if the Object a should appear after the Object b in the sorted sequence

To return to the internal comparision function, set this value to null.

I decided that instead of returning 0 if they were the same, I would just let it stick it below the item. No big deal.

Later, when checking to see if an item was selected, I used the contains() method to see if an item was in the ArrayCollection. I quickly noticed that it wasn’t finding my item, even when I knew it was in the list.  Stepping through the code, and diving in to the Flex framework classes, I found that the contains() method uses the custom sort compare method to compare the item you’re looking for with the items in the list. It determines it found the item when it gets 0 back from the compare function. Since my custom compare method never returned 0, it never found the item.

Knowing this also helped me optimize the compare method for finding items.  Instead of thinking just in terms of whether one item goes before or after another item (or at the same level), I could also think about the comparisons in terms of checking to see if itemA was itemB.