Module: MilkTea::LSP::Server::ServerFoldingRange

Included in:
MilkTea::LSP::Server
Defined in:
lib/milk_tea/lsp/server/folding_range.rb

Constant Summary collapse

BLOCK_START_PATTERN =
/\A(public\s+)?(async\s+)?(editable\s+)?(function|struct|enum|flags|variant|union|interface|if|while|for|match|unsafe|extending|defer)\b/
CONTINUATION_KEYWORDS =
%w[else elif when].freeze

Instance Method Summary collapse

Instance Method Details

#block_start?(stripped) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
# File 'lib/milk_tea/lsp/server/folding_range.rb', line 115

def block_start?(stripped)
  return false unless stripped.end_with?(":") || stripped == ":"

  stripped.match?(BLOCK_START_PATTERN) && !CONTINUATION_KEYWORDS.any? { |kw| stripped.start_with?(kw) }
end

#compute_block_folds(lines, folds) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/milk_tea/lsp/server/folding_range.rb', line 28

def compute_block_folds(lines, folds)
  stack = [] # [[start_line, indent], ...]
  last_indent = 0

  lines.each_with_index do |line, line_num|
    stripped = line.lstrip
    next if stripped.empty?

    indent = line.length - stripped.length
    stripped_no_comment = strip_comment(stripped)

    if indent < last_indent
      while stack.any? && stack.last[1] >= indent
        start_line, = stack.pop
        folds << folding_range(start_line, line_num - 1) if line_num - 1 > start_line
      end
    end

    if continuation_start?(stripped_no_comment)
      if stack.any? && stack.last[1] == indent
        start_line, = stack.pop
        folds << folding_range(start_line, line_num - 1) if line_num - 1 > start_line
      end
      stack << [line_num, indent]
    elsif block_start?(stripped_no_comment)
      stack << [line_num, indent]
    end

    last_indent = indent
  end

  last_line = lines.length - 1
  while stack.any?
    start_line, = stack.pop
    folds << folding_range(start_line, last_line) if last_line > start_line
  end
end

#compute_comment_folds(lines, folds) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/milk_tea/lsp/server/folding_range.rb', line 97

def compute_comment_folds(lines, folds)
  comment_start = nil

  lines.each_with_index do |line, line_num|
    stripped = line.lstrip
    if stripped.start_with?("#>") && comment_start.nil?
      comment_start = line_num
    elsif stripped.include?("<#") && comment_start
      folds << {
        startLine: comment_start,
        endLine: line_num,
        kind: "comment",
      }
      comment_start = nil
    end
  end
end

#compute_import_folds(lines, folds) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/milk_tea/lsp/server/folding_range.rb', line 66

def compute_import_folds(lines, folds)
  import_start = nil

  lines.each_with_index do |line, line_num|
    stripped = line.lstrip
    if stripped.start_with?("import ")
      import_start ||= line_num
    else
      if import_start && line_num - 1 > import_start
        folds << {
          startLine: import_start,
          endLine: line_num - 1,
          kind: "imports",
        }
      end
      import_start = nil
    end
  end

  return unless import_start

  last_line = lines.length - 1
  return unless last_line > import_start

  folds << {
    startLine: import_start,
    endLine: last_line,
    kind: "imports",
  }
end

#continuation_start?(stripped) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
# File 'lib/milk_tea/lsp/server/folding_range.rb', line 121

def continuation_start?(stripped)
  return false unless stripped.end_with?(":") || stripped == ":"

  CONTINUATION_KEYWORDS.any? { |kw| stripped.start_with?(kw) }
end

#folding_range(start_line, end_line) ⇒ Object



143
144
145
# File 'lib/milk_tea/lsp/server/folding_range.rb', line 143

def folding_range(start_line, end_line)
  { startLine: start_line, endLine: end_line }
end

#handle_folding_range(params) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/milk_tea/lsp/server/folding_range.rb', line 11

def handle_folding_range(params)
  text_document = params["textDocument"]
  uri = text_document["uri"]
  content = @workspace.get_content(uri)
  return [] unless content

  lines = content.split("\n", -1)
  folds = []

  compute_block_folds(lines, folds)
  compute_import_folds(lines, folds)
  compute_comment_folds(lines, folds)
  trim_trailing_blank_lines(folds, lines)

  folds
end

#strip_comment(line) ⇒ Object



127
128
129
130
131
# File 'lib/milk_tea/lsp/server/folding_range.rb', line 127

def strip_comment(line)

  idx = line.index(" #")
  idx ? line[0...idx] : line
end

#trim_trailing_blank_lines(folds, lines) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/milk_tea/lsp/server/folding_range.rb', line 133

def trim_trailing_blank_lines(folds, lines)
  folds.each do |fold|
    end_line = fold[:endLine]
    while end_line > fold[:startLine] && lines[end_line].strip.empty?
      end_line -= 1
    end
    fold[:endLine] = end_line
  end
end