Class: Redcar::Command

Inherits:
Object show all
Extended by:
Observable, Sensitive
Defined in:
plugins/application/lib/application/command.rb,
plugins/application/lib/application/command/history.rb,
plugins/application/lib/application/command/executor.rb,
plugins/application/spec/application/command/executor_spec.rb

Overview

A Redcar::Command class encapsulates a block of code, along with metadata to describe in what ways it can be called, and how Redcar will treat the command instances.

Define commands by subclassing the Redcar::Command class.

## Examples

class CloseTab < Redcar::Command
  def self.keymaps
     Redcar::Keymap.build("main", :osx) do
       link "Cmd+W",  CloseTab
     end            
  def execute
    tab.close if tab
  end
end

## Avoiding Memory Leaks (Command instance Lifecycle)

When a command is activated, an instance of the Command class will be created. Once the command has been run, the Command instance is put into the Command History, which stores recent commands so they are accessible to e.g. Macros.

This means that the command instance will hang around indefinitely, so it is easy to create a memory leak (it is not 'technically' a leak because the Command will be collected eventually, just probably not soon enough.)

For example, this command has a memory leak:

class MyCommand < Redcar::Command
  def execute
    @cache = create_outrageously_large_data_structure
    do_some_stuff_with_data_structure
  end
end

Because the History will store the MyCommand instance, the @cache object will be around for quite a while. Best to get rid of it:

class MyCommand < Redcar::Command
  def execute
    @cache = create_outrageously_large_data_structure
    do_some_stuff_with_data_structure
    @cache = nil
  end
end

Direct Known Subclasses

ProjectSearch::RefreshIndex, ProjectSearch::WordSearchCommand, Application::CloseAll, Application::CloseNotebookCommand, Application::CloseOthers, Application::CloseWindowCommand, Application::EnlargeNotebookCommand, Application::FocusWindowCommand, Application::MoveTabToOtherNotebookCommand, Application::OpenNewNotebookCommand, Application::OpenNewWindowCommand, Application::OpenUpdateCommand, Application::QuitCommand, Application::ResetNotebookWidthsCommand, Application::RotateNotebooksCommand, Application::SwitchNotebookCommand, Application::ToggleCheckForUpdatesCommand, Application::ToggleFullscreen, Application::ToggleToolbar, Application::TreebookWidthCommand, Redcar::ClipboardViewer::OpenClipboardBar, Redcar::ClipboardViewer::OpenClipboardBrowser, Debug::OpenHistoryCommand, Debug::ShowCallGraphCommand, Debug::ShowCallTreeCommand, Debug::StartProfilingCommand, Debug::StopProfilingCommand, Declarations::RebuildTagsCommand, EditView::ToggleIndentSelectionOnTabCommand, Help::OnlineHelpCommand, Help::SubmitABugCommand, Help::ViewShortcutsCommand, HtmlView::DisplayWebContent, HtmlView::ToggleBrowserBar, Macros::MacroManagerCommand, Macros::NameLastMacroCommand, MyPlugin::EditMyPluginCommand, MyPlugin::HelloWorldCommand, OpenDefaultApp::OpenDefaultAppCommand, OpenDefaultBrowserCommand, PluginManagerUi::OpenCommand, PluginManagerUi::OpenPreferencesCommand, PluginManagerUi::ReloadLastReloadedCommand, PluginManagerUi::ReloadPluginsCommand, Project::DirectoryOpenCommand, Project::FindRecentCommand, Project::OpenCommand, Project::OpenFileCommand, Project::SaveAllFilesCommand, ProjectCommand, REPL::OpenREPL, RunTestCommand, Runnables::AppendParamsAndRunCommand, Runnables::ShowRunnables, Scm::CommitMirror::CommitChangesCommand, Scm::CommitMirror::CreateCommitCommand, Scm::RemoteInitCommand, Scm::ToggleScmTreeCommand, StripTrailingSpaces::ToggleStripBlankLines, StripTrailingSpaces::ToggleStripTrailingSpaces, SyntaxCheck::ToggleGrammarChecking, SyntaxCheck::ToggleSyntaxChecking, TabCommand, TaskManager::OpenCommand, Textmate::ClearBundleMenu, Textmate::CreateNewBundle, Textmate::CreateNewSnippet, Textmate::CreateNewSnippetGroup, Textmate::DeleteNode, Textmate::InstalledBundles, Textmate::OpenBundleEditor, Textmate::OpenSnippetEditor, Textmate::PinBundleToMenu, Textmate::ReloadSnippetTree, Textmate::RemovePinnedBundle, Textmate::RenameSnippetGroup, Textmate::ShowSnippetTree, Textmate::SortNodes, TodoList::ViewTodoListCommand, Top::AboutCommand, Top::BackwardNavigationCommand, Top::ChangelogCommand, Top::ForwardNavigationCommand, Top::OpenNewEditTabCommand, Top::PrintScopeTreeCommand, Top::ShowTheme, Top::ShowTitle, TreeCommand, WebBookmarks::ShowWebBookmarksCommand

Defined Under Namespace

Classes: Executor, History

Constant Summary

Constants included from Observable

Observable::ASPECTS

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Sensitive

active?, sensitivities, sensitivity_names, sensitize

Methods included from Observable

add_listener, notify_listeners, remove_listener

Instance Attribute Details

- (Object) error

Returns the value of attribute error



50
51
52
# File 'plugins/application/lib/application/command.rb', line 50

def error
  @error
end

- (Object) tab=(value)

Sets the attribute tab

Parameters:

  • value

    the value to set the attribute tab to.



51
52
53
# File 'plugins/application/lib/application/command.rb', line 51

def tab=(value)
  @tab = value
end

Class Method Details

+ (Object) active_changed(value)

Called by the Sensitive module when the active value of this changed



62
63
64
# File 'plugins/application/lib/application/command.rb', line 62

def self.active_changed(value)
  notify_listeners(:active_changed, value)
end

+ (Object) inherited(klass)



56
57
58
59
# File 'plugins/application/lib/application/command.rb', line 56

def self.inherited(klass)
  klass.send(:extend, Redcar::Sensitive)
  klass.sensitize(*sensitivity_names)
end

+ (Object) norecord



66
67
68
# File 'plugins/application/lib/application/command.rb', line 66

def self.norecord
  @record = false
end

+ (Boolean) record?

Returns:

  • (Boolean)


70
71
72
# File 'plugins/application/lib/application/command.rb', line 70

def self.record?
  @record == nil or @record
end

Instance Method Details

- (Object) environment(env)



74
75
76
77
78
79
80
# File 'plugins/application/lib/application/command.rb', line 74

def environment(env)
  if env == nil
    remove_instance_variable(:@env) if @env
  else
    @env = env
  end
end

- (Object) inspect



91
92
93
# File 'plugins/application/lib/application/command.rb', line 91

def inspect
  "#<#{self.class.name}>"
end

- (Object) run(opts = {})



82
83
84
85
86
87
88
89
# File 'plugins/application/lib/application/command.rb', line 82

def run(opts = {})
  @executor = Executor.new(self, opts)
  s = Time.now
  result = @executor.execute
  remove_instance_variable(:@executor)
  Redcar.log.debug("command #{self.inspect} (#{Time.now - s})")
  result
end