utils 0.69.0 → 0.70.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/code_comment +126 -68
- data/lib/utils/version.rb +1 -1
- data/utils.gemspec +3 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ceaf5765e54421ff1940fb7f39c2e46a40ae1f3886b31490da70780e810cdf6
|
4
|
+
data.tar.gz: 3b1df5558bb1a352778b8c747a4a1085951a16e4dc33050a2d14ce5353402ad6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1318e0a95615867bdaff12d8a5fd88e9e8329ab997585a804e7e8f160b7ff303d33101b9f43b055ec6bbc30c93ba9c915f9e5c3c29f4d45c876377b7269225c
|
7
|
+
data.tar.gz: 6f064a83362f4e5053fb3dbb37ab1331b00865707139cc880e08f2230fac2c95bb16184814e707c8f88f87a08aa3f13b3d432cac6e706443c4feeb12a51f5c59
|
data/bin/code_comment
CHANGED
@@ -54,24 +54,18 @@ META_METHOD = /(
|
|
54
54
|
)? # Optional variant
|
55
55
|
)\s+:/x
|
56
56
|
|
57
|
-
|
58
|
-
#
|
59
|
-
# This method retrieves the source code of a method by analyzing the file
|
60
|
-
# location and line number provided. It attempts to identify the method
|
61
|
-
# definition by examining the surrounding context, including handling
|
62
|
-
# private and protected method declarations.
|
63
|
-
#
|
64
|
-
# @param filename_linenumber [ Object ] an object that responds to #source_location
|
65
|
-
# and provides filename and line number information
|
66
|
-
#
|
67
|
-
# @return [ String ] the source code of the method definition, or an empty string
|
68
|
-
# if the method cannot be determined
|
69
|
-
def fetch_method(filename_linenumber)
|
57
|
+
def fetch_construct(filename_linenumber)
|
70
58
|
result = ''
|
71
59
|
source_location = filename_linenumber.source_location
|
72
60
|
lf = Tins::LinesFile.for_filename(source_location.filename, source_location.linenumber)
|
61
|
+
if /^(?:\s*)(class|module)/ =~ lf.line
|
62
|
+
return lf.line, $1.to_sym
|
63
|
+
end
|
64
|
+
if /^(?:\s*)(?:[A-Z]\w*)\s*=\s*(Class|Module)\.new/ =~ lf.line
|
65
|
+
return lf.line, $1.downcase.to_sym
|
66
|
+
end
|
73
67
|
if lf.line =~ META_METHOD
|
74
|
-
return lf.line
|
68
|
+
return lf.line, :method
|
75
69
|
end
|
76
70
|
if spaces = lf.match_backward(/^(\s*?)(\S.*?\s+)?def\s+/)&.first
|
77
71
|
line_number_begin = lf.line_number
|
@@ -82,68 +76,132 @@ def fetch_method(filename_linenumber)
|
|
82
76
|
result << lf.line
|
83
77
|
end
|
84
78
|
end
|
85
|
-
result
|
79
|
+
return result, :method
|
80
|
+
end
|
81
|
+
|
82
|
+
def build_method_prompt(construct_type, construct, file_contents)
|
83
|
+
default_prompt = <<~EOT
|
84
|
+
Look at this code to document it:
|
85
|
+
|
86
|
+
%{file_contents}
|
87
|
+
|
88
|
+
Here's an example for how you should document a %{construct_type}.
|
89
|
+
|
90
|
+
# The foo %{construct_type} computes the bar result by processing…
|
91
|
+
# then it returns the result.
|
92
|
+
#
|
93
|
+
# @param first [ String ] the foo string
|
94
|
+
# @param second [ Integer ] the number of bars lengthy detailed mutltiline
|
95
|
+
# detailed explanation including a newline.
|
96
|
+
# @param third [ TrueClass, FalseClass ]
|
97
|
+
#
|
98
|
+
# @yield [ a, b ]
|
99
|
+
#
|
100
|
+
# @raise [ ArgumentError ] if block argument wasn't provided
|
101
|
+
# @raise [ ArgumentError ] if second parameter was too small.
|
102
|
+
#
|
103
|
+
# @return [ ProcessResult ]
|
104
|
+
|
105
|
+
And this is the %{construct_type} you should document:
|
106
|
+
|
107
|
+
%{construct}
|
108
|
+
|
109
|
+
Output a YARD comment for this %{construct_type}:
|
110
|
+
|
111
|
+
Format requirements:
|
112
|
+
|
113
|
+
1. Focus on providing a description of the %{construct_type}'s purpose
|
114
|
+
without including any code snippets.
|
115
|
+
2. You should omit the @raise if you are not sure.
|
116
|
+
3. Never use `, `ruby, ```, ```ruby in your response.
|
117
|
+
4. Never add any other remarks or explanation to your response.
|
118
|
+
5. Start each line of your comment with a single # character.
|
119
|
+
EOT
|
120
|
+
$config.read('method-prompt.txt', default: default_prompt) % {
|
121
|
+
construct_type:, construct:, file_contents:,
|
122
|
+
}
|
123
|
+
end
|
124
|
+
|
125
|
+
def build_class_module_prompt(construct_type, construct, file_contents)
|
126
|
+
default_prompt = <<~EOT
|
127
|
+
Look at this code to document it:
|
128
|
+
|
129
|
+
%{file_contents}
|
130
|
+
|
131
|
+
Here's an example for how you should document a %{construct_type}.
|
132
|
+
|
133
|
+
# A brief description of what this %{construct_type} does.
|
134
|
+
#
|
135
|
+
# @example
|
136
|
+
# MyClass.new do |obj|
|
137
|
+
# obj.method
|
138
|
+
# end
|
139
|
+
|
140
|
+
And this is the %{construct_type} you should document:
|
141
|
+
|
142
|
+
%{construct}
|
143
|
+
|
144
|
+
Output a YARD comment for this %{construct_type}:
|
145
|
+
|
146
|
+
Format requirements:
|
147
|
+
|
148
|
+
1. Focus on providing a description of the %{construct_type}'s purpose
|
149
|
+
without including any code snippets.
|
150
|
+
2. Include @example tag if there are notable usage patterns for this
|
151
|
+
construct.
|
152
|
+
3. Never use `, `ruby, ```, ```ruby in your response.
|
153
|
+
4. Never add any other remarks or explanation to your response.
|
154
|
+
5. Start each line of your comment with a single # character.
|
155
|
+
EOT
|
156
|
+
$config.read('class-module-prompt.txt', default: default_prompt) % {
|
157
|
+
construct_type:, construct:, file_contents:,
|
158
|
+
}
|
159
|
+
end
|
160
|
+
|
161
|
+
def build_prompt(construct_type, construct, file_contents)
|
162
|
+
case construct_type
|
163
|
+
when :method
|
164
|
+
build_method_prompt(construct_type, construct, file_contents)
|
165
|
+
when :class, :module
|
166
|
+
build_class_module_prompt(construct_type, construct, file_contents)
|
167
|
+
else
|
168
|
+
fail "unknown construct_type #{construct_type.inspect}"
|
169
|
+
end
|
86
170
|
end
|
87
171
|
|
88
172
|
filename_linenumber = ARGV.shift or fail "require file_name as second argument"
|
89
|
-
config
|
90
|
-
method = fetch_method(filename_linenumber)
|
91
|
-
method_indent = method[/\A( *)/, 1].size
|
173
|
+
$config = Utils::ConfigDir.new('code_comment', env_var: 'XDG_CONFIG_HOME')
|
92
174
|
files = Dir['{lib,spec,test}/**/*.rb']
|
93
175
|
base_url = ENV['OLLAMA_URL'] || 'http://%s' % ENV.fetch('OLLAMA_HOST')
|
94
176
|
model = ENV.fetch('OLLAMA_MODEL', 'llama3.1')
|
177
|
+
construct, construct_type = fetch_construct(filename_linenumber)
|
178
|
+
construct_indent = construct[/\A( *)/, 1].size
|
179
|
+
file_contents = files.map { _1 + ":\n" + File.read(_1) } * ?\n
|
95
180
|
#call_sites = %x(cscope -L -3 "#{method_name}" $(find . -name '*.rb') | awk '{ print $1 ":" $3 }').lines.map(&:chomp).uniq
|
96
181
|
#methods = call_sites.map { fetch_method(_1) } * ?\n
|
97
182
|
|
98
|
-
default_system =
|
99
|
-
|
100
|
-
description of its purpose, parameters, raised exceptions (and why), and
|
101
|
-
return values, assuming an expert-level understanding of the programming
|
102
|
-
concepts involved.
|
103
|
-
EOT
|
104
|
-
system = config.read('system.txt', default: default_system)
|
183
|
+
default_system = <<~EOT
|
184
|
+
**Guidelines**
|
105
185
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
#
|
122
|
-
# @yield [ a, b ]
|
123
|
-
#
|
124
|
-
# @raise [ ArgumentError ] if block argument wasn't provided
|
125
|
-
# @raise [ ArgumentError ] if second parameter was too small.
|
126
|
-
#
|
127
|
-
# @return [ ProcessResult ]
|
128
|
-
|
129
|
-
And this is the method you should document:
|
130
|
-
|
131
|
-
%{method}
|
132
|
-
|
133
|
-
Output a YARD comment for this method:
|
134
|
-
|
135
|
-
Format requirements:
|
136
|
-
|
137
|
-
1. Focus on providing a description of the method's purpose
|
138
|
-
without including any code snippets.
|
139
|
-
2. You should ommit the @raise if you are not sure.
|
140
|
-
3. Never use `, `ruby, ```, ```ruby in your response.
|
141
|
-
4. Never add any other remarks or explanation to your response.
|
142
|
-
5. Start each line of your comment with a single # character.
|
186
|
+
- When answering, assume an expert-level understanding of the programming
|
187
|
+
concepts involved.
|
188
|
+
|
189
|
+
- If given a **method**, provide high-quality YARD comments including:
|
190
|
+
* A brief description of its purpose
|
191
|
+
* Parameter documentation (@param)
|
192
|
+
* Return value documentation (@return)
|
193
|
+
* Exception documentation (@raise) when appropriate
|
194
|
+
* Avoid version information (@since)
|
195
|
+
|
196
|
+
- If given a **class or module**, provide high-quality YARD comments including:
|
197
|
+
* A brief description of its purpose
|
198
|
+
* Public interface documentation
|
199
|
+
* @example usage patterns when helpful
|
200
|
+
* Avoid version information (@since)
|
143
201
|
EOT
|
144
|
-
|
145
|
-
|
146
|
-
|
202
|
+
system = $config.read('system.txt', default: default_system)
|
203
|
+
|
204
|
+
prompt = build_prompt(construct_type, construct, file_contents)
|
147
205
|
|
148
206
|
default_options = JSON(
|
149
207
|
#repeat_penalty: 1.8,
|
@@ -156,10 +214,10 @@ default_options = JSON(
|
|
156
214
|
min_p: 0.1,
|
157
215
|
)
|
158
216
|
|
159
|
-
client_config = Client::Config.load_from_json config + 'client.json'
|
217
|
+
client_config = Client::Config.load_from_json $config + 'client.json'
|
160
218
|
client_config.base_url = base_url
|
161
219
|
ollama = Client.configure_with(client_config)
|
162
|
-
options = JSON.parse(config.read('options.json', default: default_options))
|
220
|
+
options = JSON.parse($config.read('options.json', default: default_options))
|
163
221
|
|
164
222
|
if ENV['DEBUG'].to_i == 1
|
165
223
|
File.open('debug.log', ?w) do |log|
|
@@ -171,4 +229,4 @@ if ENV['DEBUG'].to_i == 1
|
|
171
229
|
end
|
172
230
|
|
173
231
|
response = ollama.generate(model:, system:, prompt:, options:, stream: false, think: false).response
|
174
|
-
puts response.gsub(/^/) { ' ' *
|
232
|
+
puts response.gsub(/^/) { ' ' * construct_indent }
|
data/lib/utils/version.rb
CHANGED
data/utils.gemspec
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: utils 0.
|
2
|
+
# stub: utils 0.70.0 ruby lib
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "utils".freeze
|
6
|
-
s.version = "0.
|
6
|
+
s.version = "0.70.0".freeze
|
7
7
|
|
8
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
9
9
|
s.require_paths = ["lib".freeze]
|
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
|
|
23
23
|
|
24
24
|
s.specification_version = 4
|
25
25
|
|
26
|
-
s.add_development_dependency(%q<gem_hadar>.freeze, ["~>
|
26
|
+
s.add_development_dependency(%q<gem_hadar>.freeze, ["~> 2.0".freeze])
|
27
27
|
s.add_development_dependency(%q<test-unit>.freeze, [">= 0".freeze])
|
28
28
|
s.add_runtime_dependency(%q<unix_socks>.freeze, [">= 0".freeze])
|
29
29
|
s.add_runtime_dependency(%q<webrick>.freeze, [">= 0".freeze])
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.70.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Frank
|
@@ -15,14 +15,14 @@ dependencies:
|
|
15
15
|
requirements:
|
16
16
|
- - "~>"
|
17
17
|
- !ruby/object:Gem::Version
|
18
|
-
version: '
|
18
|
+
version: '2.0'
|
19
19
|
type: :development
|
20
20
|
prerelease: false
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
22
22
|
requirements:
|
23
23
|
- - "~>"
|
24
24
|
- !ruby/object:Gem::Version
|
25
|
-
version: '
|
25
|
+
version: '2.0'
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: test-unit
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|