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

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

Overview

Prefer changing selection in bulk instead of modifying selection within loops. It’s much faster to change the selection in bulk. UI updates are triggered when you update the selection, so reduce the amount of calls.

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 =
'Prefer changing selection in bulk instead of modifying '\
'selection within loops.'

Constants inherited from SketchUp::Cop

SketchUp::Cop::SKETCHUP_DEPARTMENT_SEVERITY

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)


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

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



69
70
71
72
73
74
# File 'lib/rubocop/sketchup/cop/performance/selection_bulk.rb', line 69

def on_send(node)
  return unless selection?(node)
  return unless node.ancestors.any?(&method(:iterator?))

  add_offense(node, location: range_with_receiver(node))
end