Class: RuboCop::Cop::SketchupPerformance::SelectionBulkChanges

Inherits:
SketchUp::Cop
  • Object
show all
Defined in:
lib/rubocop/sketchup/cop/performance/selection_bulk.rb

Overview

Avoid modifying the selection set within loops. It's much faster to change the selection in bulk.

Examples:

Poor performance

model = Sketchup.active_model
model.active_entities.each { |entity|
  model.selection.add(entity) if entity.is_a?(Sketchup::Face)
}

Better performance

model = Sketchup.active_model
faces = model.active_entities.map { |entity|
  entity.is_a?(Sketchup::Face)
}
model.selection.add(faces)

Better performance and simpler

model = Sketchup.active_model
faces = model.active_entities.grep(Sketchup::Face)
model.selection.add(faces)

Constant Summary collapse

MSG =
'Avoid modifying selecting within loops.'.freeze

Constants included from SketchUp::Config

SketchUp::Config::DEFAULT_CONFIGURATION

Instance Method Summary collapse

Methods inherited from SketchUp::Cop

inherited, #relevant_file?

Instance Method Details

#iterator?(node) ⇒ Boolean

Returns:

  • (Boolean)

56
57
58
59
60
61
62
# File 'lib/rubocop/sketchup/cop/performance/selection_bulk.rb', line 56

def iterator?(node)
  node.is_a?(RuboCop::AST::ForNode) ||
    node.is_a?(RuboCop::AST::UntilNode) ||
    node.is_a?(RuboCop::AST::WhileNode) ||
    block_loop?(node) ||
    numeric_loop?(node)
end

#on_send(node) ⇒ Object


64
65
66
67
68
# File 'lib/rubocop/sketchup/cop/performance/selection_bulk.rb', line 64

def on_send(node)
  return unless selection?(node)
  return unless node.ancestors.any?(&method(:iterator?))
  add_offense(node, location: :expression)
end