Class: PuppetLanguageServer::Manifest::FoldingProvider
- Inherits:
-
Object
- Object
- PuppetLanguageServer::Manifest::FoldingProvider
- Defined in:
- lib/puppet-languageserver/manifest/folding_provider.rb
Constant Summary collapse
- REGION_NONE =
nil
- REGION_COMMENT =
'comment'
- REGION_REGION =
'region'
Class Method Summary collapse
Instance Method Summary collapse
- #end_region?(text) ⇒ Boolean
- #folding_ranges(tokens, show_last_line = false) ⇒ Object
- #start_region?(text) ⇒ Boolean
Class Method Details
.instance ⇒ Object
10 11 12 |
# File 'lib/puppet-languageserver/manifest/folding_provider.rb', line 10 def instance @instance ||= new end |
.supported? ⇒ Boolean
14 15 16 17 18 |
# File 'lib/puppet-languageserver/manifest/folding_provider.rb', line 14 def supported? # Folding is only supported on Puppet 6.3.0 and above # Requires - https://github.com/puppetlabs/puppet/commit/6d375ab4d735779031d49ab8631bd9d161a9c3e3 @supported ||= Gem::Version.new(Puppet.version) >= Gem::Version.new('6.3.0') end |
Instance Method Details
#end_region?(text) ⇒ Boolean
29 30 31 |
# File 'lib/puppet-languageserver/manifest/folding_provider.rb', line 29 def end_region?(text) !(text =~ /^#\s*endregion\b/).nil? end |
#folding_ranges(tokens, show_last_line = false) ⇒ Object
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 65 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 |
# File 'lib/puppet-languageserver/manifest/folding_provider.rb', line 33 def folding_ranges(tokens, show_last_line = false) return nil unless self.class.supported? ranges = {} brace_stack = [] brack_stack = [] comment_stack = [] index = 0 until index > tokens.length - 1 case tokens[index][0] # Find comments when :TOKEN_COMMENT if block_comment?(index, tokens) comment = tokens[index][1].locator.extract_text(tokens[index][1].offset, tokens[index][1].length) if start_region?(comment) # rubocop:disable Metrics/BlockNesting comment_stack.push(tokens[index][1]) elsif end_region?(comment) && !comment_stack.empty? # rubocop:disable Metrics/BlockNesting add_range!(create_range_span_tokens(comment_stack.pop, tokens[index][1], REGION_REGION), ranges) else index = process_block_comment!(index, tokens, ranges) end end # Find old style comments /* -> */ when :TOKEN_MLCOMMENT add_range!(create_range_whole_token(tokens[index][1], REGION_COMMENT), ranges) # Find matching braces { -> } and select brace ?{ -> } when :LBRACE, :SELBRACE brace_stack.push(tokens[index][1]) when :RBRACE add_range!(create_range_span_tokens(brace_stack.pop, tokens[index][1], REGION_NONE), ranges) unless brace_stack.empty? # Find matching braces [ -> ], list and index when :LISTSTART, :LBRACK brack_stack.push(tokens[index][1]) when :RBRACK add_range!(create_range_span_tokens(brack_stack.pop, tokens[index][1], REGION_NONE), ranges) unless brack_stack.empty? # Find matching Heredoc and heredoc sublocations when :HEREDOC # Need to check if the next token is :SUBLOCATE if index < tokens.length - 2 && tokens[index + 1][0] == :SUBLOCATE # rubocop:disable Style/IfUnlessModifier add_range!(create_range_heredoc(tokens[index][1], tokens[index + 1][1], REGION_NONE), ranges) end end index += 1 end # If we are showing the last line then decrement the EndLine by one, if possible if show_last_line ranges.values.each do |range| range.endLine = [range.startLine, range.endLine - 1].max range.endCharacter = 0 # We don't know where the previous line actually ends so set it to zero end end ranges.values end |
#start_region?(text) ⇒ Boolean
25 26 27 |
# File 'lib/puppet-languageserver/manifest/folding_provider.rb', line 25 def start_region?(text) !(text =~ /^#\s*region\b/).nil? end |