Skip to content

Commit

Permalink
(GH-289) Make Format On Type file size configurable
Browse files Browse the repository at this point in the history
Previously only files that were less than 4KB in size could use the
format-on-type feature. This commit updates the Language Server to honor a
new configuration setting puppet.editorService.formatOnType.maxFileSize which
will determine the maximum file size.  A size of 0 will disable the size check.
The default is 4096 as per the previous behaviour.

No specific tests have been added and has been manually tested.
  • Loading branch information
glennsarti committed Jan 13, 2021
1 parent 83b8652 commit 3e08c07
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/puppet-languageserver/manifest/format_on_type_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ def instance
end
end

def format(content, line, char, trigger_character, formatting_options)
def format(content, line, char, trigger_character, formatting_options, max_filesize = 4096)
result = []
# Abort if the user has pressed something other than `>`
return result unless trigger_character == '>'
# Abort if the formatting is tab based. Can't do that yet
return result unless formatting_options['insertSpaces'] == true
# Abort if content is too big
return result if content.length > 4096
unless max_filesize.zero?
return result if content.length > max_filesize
end

lexer = PuppetLint::Lexer.new
tokens = lexer.tokenise(content)
Expand Down
3 changes: 2 additions & 1 deletion lib/puppet-languageserver/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ def request_textdocument_ontypeformatting(_, json_rpc_message)
line_num,
char_num,
json_rpc_message.params['ch'],
json_rpc_message.params['options']
json_rpc_message.params['options'],
language_client.format_on_type_filesize_limit
)
else
raise "Unable to format on type on #{file_uri}"
Expand Down
17 changes: 17 additions & 0 deletions lib/puppet-languageserver/session_state/language_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class LanguageClient

# Client settings
attr_reader :format_on_type
attr_reader :format_on_type_filesize_limit
attr_reader :use_puppetfile_resolver

def initialize(message_handler)
Expand All @@ -25,6 +26,7 @@ def initialize(message_handler)

# Default settings
@format_on_type = false
@format_on_type_filesize_limit = 4096
@use_puppetfile_resolver = true
end

Expand Down Expand Up @@ -58,6 +60,9 @@ def parse_lsp_configuration_settings!(settings = {})
end
@format_on_type = value
end
# format on type file size
value = safe_hash_traverse(settings, 'puppet', 'editorService', 'formatOnType', 'maxFileSize')
@format_on_type_filesize_limit = to_integer(value, 4096, 0)
# use puppetfile resolver
value = safe_hash_traverse(settings, 'puppet', 'validate', 'resolvePuppetfiles')
@use_puppetfile_resolver = to_boolean(value)
Expand Down Expand Up @@ -174,6 +179,18 @@ def to_boolean(value)
value.to_s =~ %r{^(true|t|yes|y|1)$/i}
end

def to_integer(value, default = nil, min = nil, max = nil)
return default if value.nil?
begin
intv = Integer(value)
return default if !min.nil? && intv < min
return default if !max.nil? && intv > max
intv
rescue ArgumentError
default
end
end

def new_request_id
SecureRandom.uuid
end
Expand Down

0 comments on commit 3e08c07

Please sign in to comment.