Module: Isort::WrapModes

Defined in:
lib/isort/wrap_modes.rb

Overview

Wrap modes for handling long import lines

Constant Summary collapse

GRID =

Mode 0: Grid - wraps with opening parenthesis on first line require 'a', 'b', 'c', 'd', 'e'

0
VERTICAL =

Mode 1: Vertical - one import per line after opening paren require( 'a', 'b', 'c', )

1
HANGING_INDENT =

Mode 2: Hanging indent - continuation lines have extra indent require 'a', 'b', 'c', 'd', 'e'

2
VERTICAL_HANGING_INDENT =

Mode 3: Vertical hanging indent - combines vertical and hanging require( 'a', 'b', 'c', )

3
VERTICAL_GRID =

Mode 4: Vertical grid - like grid but vertical require( 'a', 'b', 'c', 'd', )

4
VERTICAL_GRID_GROUPED =

Mode 5: Vertical grid grouped - groups related imports require('a', 'b', 'c', 'd')

5
VERTICAL_GRID_GROUPED_NO_WRAP =

Mode 6: Vertical grid grouped with trailing comma

6
NOQA =

Mode 7: NOQA - single line with comment to disable linting

7
DEFAULT =

Default mode

VERTICAL_HANGING_INDENT
MODES =

All available modes

{
  grid: GRID,
  vertical: VERTICAL,
  hanging_indent: HANGING_INDENT,
  vertical_hanging_indent: VERTICAL_HANGING_INDENT,
  vertical_grid: VERTICAL_GRID,
  vertical_grid_grouped: VERTICAL_GRID_GROUPED,
  vertical_grid_grouped_no_wrap: VERTICAL_GRID_GROUPED_NO_WRAP,
  noqa: NOQA
}.freeze

Class Method Summary collapse

Class Method Details

.format_line(imports, mode: DEFAULT, line_length: 79, indent: "") ⇒ Object

Format a multi-import line according to the wrap mode for formatting long autoload statements or future multi-require support



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/isort/wrap_modes.rb', line 80

def format_line(imports, mode: DEFAULT, line_length: 79, indent: "")
  mode = get(mode)

  case mode
  when GRID
    format_grid(imports, line_length, indent)
  when VERTICAL
    format_vertical(imports, indent)
  when HANGING_INDENT
    format_hanging_indent(imports, line_length, indent)
  when VERTICAL_HANGING_INDENT
    format_vertical_hanging_indent(imports, indent)
  when VERTICAL_GRID
    format_vertical_grid(imports, line_length, indent)
  when VERTICAL_GRID_GROUPED
    format_vertical_grid_grouped(imports, line_length, indent)
  when NOQA
    format_noqa(imports, indent)
  else
    format_vertical_hanging_indent(imports, indent)
  end
end

.get(mode) ⇒ Object

Get mode by name or number



67
68
69
70
71
72
73
74
75
76
# File 'lib/isort/wrap_modes.rb', line 67

def get(mode)
  case mode
  when Integer
    mode
  when Symbol, String
    MODES[mode.to_sym] || DEFAULT
  else
    DEFAULT
  end
end

.needs_wrapping?(line, max_length: 79) ⇒ Boolean

Check if a line needs wrapping based on line length

Returns:

  • (Boolean)


104
105
106
# File 'lib/isort/wrap_modes.rb', line 104

def needs_wrapping?(line, max_length: 79)
  line.length > max_length
end

.wrap_comment(comment, max_length: 79, indent: "") ⇒ Object

Split a long import line into multiple lines Currently used for comments/documentation, as Ruby imports are typically single-line



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/isort/wrap_modes.rb', line 110

def wrap_comment(comment, max_length: 79, indent: "")
  return [comment] if comment.length <= max_length

  words = comment.split(/\s+/)
  lines = []
  current_line = indent.dup

  words.each do |word|
    if current_line.length + word.length + 1 > max_length && current_line != indent
      lines << current_line.rstrip
      current_line = "#{indent}# #{word}"
    else
      current_line += current_line == indent ? "# #{word}" : " #{word}"
    end
  end

  lines << current_line.rstrip unless current_line.strip.empty?
  lines
end