dpu 0.4.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 226d037ff45361a6b68bd6626166b3d70d818e89ec7f6adbd9b11b97f143948b
4
- data.tar.gz: 9440a89d950b210f409b1d776d2a03c2ee203e2fa52910f59dd7cf004be1abcb
3
+ metadata.gz: 590a2cd53e8775d56f0b470b0407789f1d7bf7ee47c2b3c2f0f8fcb19d1805e7
4
+ data.tar.gz: dfa3ef6408df75dcfd83712a65c6a2020b5949c164d93c99ec85328530ffe1a7
5
5
  SHA512:
6
- metadata.gz: c658a1fc92c4939456eb11f2dee2d4e350a325a4a212705bef1007f3312678f9872edddca2fa4629840548ef4490baf09ed99b6fb0182336ffadf2ef073e2582
7
- data.tar.gz: 1bf97e092add6701a49ce53d7a4057d00f953ecb47f46512fcc918a8c51dff95d7cab5552e2148dd7fc3c9d89835e2e92abce9647d2073c131a12336d70ba51a
6
+ metadata.gz: '08e259ce385484109aa453fa7c30e61765aae3721e947c729535f8a53814c17ebcd62ca8f31809ecd6c7396594d59dbd91f98c9682e28bc3f0736d51cd61986a'
7
+ data.tar.gz: 56234fd78513c30ccfbe571dacafffc010a7572f40c25ff87a6656e69c1e8941ea756ab45470ad2a06196f666d4582c1cca219d0d5b217153444efc9fde0c709
data/README.md CHANGED
@@ -39,35 +39,62 @@ $ dpu path_to_code start_line_number end_line_number
39
39
 
40
40
  Write following code to your `.emacs`, and evaluate it.
41
41
 
42
+ <details>
43
+
44
+ <summary>.emacs sample</summary>
45
+
42
46
  ```emacs-lisp
43
47
  (define-key global-map (kbd "C-x L")
44
48
  (lambda ()
45
49
  (interactive)
46
- (message
47
- (concat
48
- "Copied: "
49
- (kill-new
50
- (s-chomp
51
- (shell-command-to-string
52
- (concat
53
- "dpu "
54
- buffer-file-name
55
- " "
56
- (number-to-string (line-number-at-pos (region-beginning)))
57
- (if mark-active (concat " " (number-to-string (line-number-at-pos (region-end)))))
58
- )
59
- )))))))
50
+ (save-window-excursion
51
+ (let* ((start-line-number (line-number-at-pos (if (region-active-p) (region-beginning))))
52
+ (end-line-number
53
+ (if mark-active
54
+ (line-number-at-pos
55
+ (+ (region-end)
56
+ (if (= (line-beginning-position) (region-end)) -1 0)))
57
+ ))
58
+ (proc (apply
59
+ #'start-process
60
+ "dpu" "*Dpu Command Result*"
61
+ "dpu" buffer-file-name
62
+ (number-to-string start-line-number)
63
+ (if end-line-number
64
+ (list (number-to-string end-line-number)))
65
+ ))
66
+ (kill-new-with-buffer-string
67
+ (lambda (process signal)
68
+ (when (memq (process-status process) '(exit signal))
69
+ (let* ((buf (process-buffer process)))
70
+ (with-current-buffer buf
71
+ (kill-new (s-chomp (buffer-string)))
72
+ (message "Copied: %s" (current-kill 0 t))
73
+ (kill-buffer buf)
74
+ )))))
75
+ )
76
+ (run-with-timer 10 nil (lambda (process) (kill-buffer (process-buffer process))) proc)
77
+ (if (process-live-p proc)
78
+ (set-process-sentinel proc kill-new-with-buffer-string))
79
+ )
80
+ )))
60
81
  ```
61
82
 
83
+ </details>
84
+
62
85
  Then type `C-x L` to copy permanent URI. `C-y` to paste it.
63
86
 
87
+ ---
88
+
89
+ Original asynchronous execution idea is made by [@mk2](https://github.com/mk2) ([#20](https://github.com/nishidayuya/dpu/pull/20)). Thanks! :tada:
90
+
64
91
  ### Textbringer integration
65
92
 
66
93
  ```ruby
67
94
  define_command(:copy_permanent_uri, doc: "Copy permanent URI") do
68
95
  require "dpu"
69
96
  b = Buffer.current
70
- uri = Dpu.determine_permanent_uri(Pathname(b.file_name), b.current_line)
97
+ uri = Dpu.determine_permanent_uri(Pathname(b.file_name), start_line_number: b.current_line)
71
98
  KILL_RING.push(uri)
72
99
  Clipboard.copy(uri) if CLIPBOARD_AVAILABLE
73
100
  message("Copied: #{uri}")
@@ -76,6 +103,10 @@ end
76
103
  GLOBAL_MAP.define_key("\C-xL", :copy_permanent_uri)
77
104
  ```
78
105
 
106
+ ### Visual Studio Code integration
107
+
108
+ See [Copy permanent URL](https://marketplace.visualstudio.com/items?itemName=nishidayuya.copy-permanent-url) extension on Visual Studio Marketplace.
109
+
79
110
  ### Other editor integration
80
111
 
81
112
  Write pull-request or issue, please!
data/lib/dpu/cli.rb CHANGED
@@ -15,7 +15,11 @@ class Dpu::Cli
15
15
  start_line_number = s_start_line_number&.to_i
16
16
  end_line_number = s_end_line_number&.to_i
17
17
 
18
- uri = Dpu.determine_permanent_uri(path, start_line_number, end_line_number)
18
+ uri = Dpu.determine_permanent_uri(
19
+ path,
20
+ start_line_number: start_line_number,
21
+ end_line_number: end_line_number,
22
+ )
19
23
  puts(uri)
20
24
  end
21
25
  end
@@ -0,0 +1,25 @@
1
+ class Dpu::ScmService::Base
2
+ def determine_repository_uri(repository_http_or_ssh_url)
3
+ md = self.class::REMOTE_URL_PATTERN.match(repository_http_or_ssh_url)
4
+ return nil if !md
5
+
6
+ url = self.class::REPOSITORY_URI_TEMPLATE % {
7
+ account_name: md[:account_name],
8
+ repository_name: md[:repository_name],
9
+ }
10
+ return URI(url)
11
+ end
12
+
13
+ def ref_prefix
14
+ return self.class::REF_PREFIX
15
+ end
16
+
17
+ def determine_fragment(start_line_number, end_line_number)
18
+ return nil if !start_line_number
19
+ return "L#{start_line_number}" if !end_line_number || start_line_number == end_line_number
20
+ return self.class::START_AND_END_LINE_NUMBER_FRAGMENT_TEMPLATE % {
21
+ start_line_number: start_line_number,
22
+ end_line_number: end_line_number,
23
+ }
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+ class Dpu::ScmService::Github < Dpu::ScmService::Base
2
+ REPOSITORY_URI_TEMPLATE = "https://github.com/%{account_name}/%{repository_name}"
3
+
4
+ REMOTE_URL_PATTERN = [
5
+ %r{\Agit://github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
6
+ %r{\Ahttps?://github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
7
+ %r{\Agit@github\.com:(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
8
+ %r{\Assh://git@github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
9
+ ].then { |patterns|
10
+ Regexp.union(*patterns)
11
+ }
12
+
13
+ REF_PREFIX = "blob"
14
+
15
+ START_AND_END_LINE_NUMBER_FRAGMENT_TEMPLATE = "L%{start_line_number}-L%{end_line_number}"
16
+ end
17
+
18
+ Dpu::SCM_SERVICES << Dpu::ScmService::Github.new
@@ -0,0 +1,16 @@
1
+ class Dpu::ScmService::Sourcehut < Dpu::ScmService::Base
2
+ REPOSITORY_URI_TEMPLATE = "https://git.sr.ht/~%{account_name}/%{repository_name}"
3
+
4
+ REMOTE_URL_PATTERN = [
5
+ %r{\Ahttps://git\.sr\.ht/\~(?<account_name>[^/]+)/(?<repository_name>.+)},
6
+ %r{\Agit@git\.sr\.ht:\~(?<account_name>[^/]+)/(?<repository_name>.+)},
7
+ ].then { |patterns|
8
+ Regexp.union(*patterns)
9
+ }
10
+
11
+ REF_PREFIX = "tree"
12
+
13
+ START_AND_END_LINE_NUMBER_FRAGMENT_TEMPLATE = "L%{start_line_number}-%{end_line_number}"
14
+ end
15
+
16
+ Dpu::SCM_SERVICES << Dpu::ScmService::Sourcehut.new
@@ -0,0 +1,2 @@
1
+ module Dpu::ScmService
2
+ end
data/lib/dpu/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dpu
4
- VERSION = "0.4.3"
4
+ VERSION = "0.6.0"
5
5
  end
data/lib/dpu.rb CHANGED
@@ -8,53 +8,24 @@ module Dpu
8
8
  autoload :Cli, "dpu/cli"
9
9
  autoload :VERSION, "dpu/vesion"
10
10
 
11
+ SCM_SERVICES = []
12
+
11
13
  class << self
12
- GITHUB_REPOSITORY_URI_TEMPLATE = "https://github.com/%{account_name}/%{repository_name}"
13
- SOURCEHUT_REPOSITORY_URI_TEMPLATE = "https://git.sr.ht/~%{account_name}/%{repository_name}"
14
- REPOSITORY_URI_TEMPLATES = {
15
- github: GITHUB_REPOSITORY_URI_TEMPLATE,
16
- sourcehut: SOURCEHUT_REPOSITORY_URI_TEMPLATE,
17
- }
18
-
19
- GITHUB_REMOTE_URL_PATTERN = [
20
- %r{\Agit://github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
21
- %r{\Ahttps?://github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
22
- %r{\Agit@github\.com:(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
23
- %r{\Assh://git@github\.com/(?<account_name>[^/]+)/(?<repository_name>[^/]+(?=\.git)|[^/]+)},
24
- ].then { |patterns|
25
- Regexp.union(*patterns)
26
- }
27
- SOURCEHUT_REMOTE_URL_PATTERN = [
28
- %r{\Ahttps://git\.sr\.ht/\~(?<account_name>[^/]+)/(?<repository_name>.+)},
29
- %r{\Agit@git\.sr\.ht:\~(?<account_name>[^/]+)/(?<repository_name>.+)},
30
- ].then { |patterns|
31
- Regexp.union(*patterns)
32
- }
33
- REMOTE_URL_PATTERNS = {
34
- github: GITHUB_REMOTE_URL_PATTERN,
35
- sourcehut: SOURCEHUT_REMOTE_URL_PATTERN,
36
- }
37
-
38
- REF_PREFIX = {
39
- github: 'blob',
40
- sourcehut: 'tree',
41
- }
42
-
43
- def determine_permanent_uri(path_or_link, start_line_number = nil, end_line_number = nil)
14
+ def determine_permanent_uri(path_or_link, start_line_number: nil, end_line_number: nil, max_find_version: 20)
44
15
  path = path_or_link.realpath
45
16
  relative_path = determine_relative_path(path)
46
17
 
47
18
  remote_url = get_remote_url(path)
48
- vcs_service = determine_vcs_service(remote_url)
19
+ scm_service, repository_uri = determine_scm_service_and_repository_uri(remote_url)
49
20
 
50
21
  permanent_uri_parts = [
51
- determine_repository_uri(vcs_service, remote_url),
52
- REF_PREFIX[vcs_service],
53
- find_same_content_version(path, relative_path) || determine_commit_id(path),
22
+ repository_uri,
23
+ scm_service.ref_prefix,
24
+ find_same_content_version(path, relative_path, max_find_version) || determine_commit_id(path),
54
25
  relative_path,
55
26
  ]
56
27
  permanent_uri = URI(permanent_uri_parts.join("/"))
57
- permanent_uri.fragment = determine_fragment(vcs_service, start_line_number, end_line_number)
28
+ permanent_uri.fragment = scm_service.determine_fragment(start_line_number, end_line_number)
58
29
  return permanent_uri
59
30
  end
60
31
 
@@ -79,28 +50,13 @@ module Dpu
79
50
  return run_command("git remote get-url origin", chdir: path.dirname).chomp
80
51
  end
81
52
 
82
- def determine_vcs_service(repository_http_or_ssh_url)
83
- if GITHUB_REMOTE_URL_PATTERN.match?(repository_http_or_ssh_url)
84
- return :github
85
- end
86
- if SOURCEHUT_REMOTE_URL_PATTERN.match?(repository_http_or_ssh_url)
87
- return :sourcehut
88
- end
89
-
90
- raise "unknown VCS service: #{repository_http_or_ssh_url}"
91
- end
92
-
93
- def determine_repository_uri(vcs_service, repository_http_or_ssh_url)
94
- md = REMOTE_URL_PATTERNS[vcs_service].match(repository_http_or_ssh_url)
95
- if !md
96
- return URI(repository_http_or_ssh_url)
53
+ def determine_scm_service_and_repository_uri(repository_http_or_ssh_url)
54
+ SCM_SERVICES.each do |scm_service|
55
+ repository_uri = scm_service.determine_repository_uri(repository_http_or_ssh_url)
56
+ return scm_service, repository_uri if repository_uri
97
57
  end
98
58
 
99
- url = REPOSITORY_URI_TEMPLATES[vcs_service] % {
100
- account_name: md[:account_name],
101
- repository_name: md[:repository_name],
102
- }
103
- return URI(url)
59
+ raise "unknown SCM service: #{repository_http_or_ssh_url}"
104
60
  end
105
61
 
106
62
  def determine_commit_id(path)
@@ -109,12 +65,12 @@ module Dpu
109
65
  return commit_id
110
66
  end
111
67
 
112
- def find_same_content_version(path, relative_path_from_repository_root)
68
+ def find_same_content_version(path, relative_path_from_repository_root, max_find_version)
113
69
  stdout = run_command(*%w[git tag --list [0-9]* v[0-9]*], chdir: path.dirname)
114
70
  versions = VersionSorter.sort(stdout.each_line(chomp: true).select(&:ascii_only?))
115
71
 
116
72
  content_in_head = path.read
117
- same_content_version = versions.reverse_each.find { |version|
73
+ same_content_version = versions.reverse_each.take(max_find_version).find { |version|
118
74
  content_in_version, _stderr, _status = *Open3.capture3(
119
75
  *%W[git show #{version}:#{relative_path_from_repository_root}],
120
76
  chdir: path.dirname,
@@ -123,12 +79,10 @@ module Dpu
123
79
  }
124
80
  return same_content_version
125
81
  end
126
-
127
- def determine_fragment(vcs_service, start_line_number, end_line_number)
128
- return nil if !start_line_number
129
- return "L#{start_line_number}" if !end_line_number || start_line_number == end_line_number
130
- return "L#{start_line_number}-L#{end_line_number}" if vcs_service == :github
131
- return "L#{start_line_number}-#{end_line_number}" if vcs_service == :sourcehut
132
- end
133
82
  end
134
83
  end
84
+
85
+ require "dpu/scm_service"
86
+ require "dpu/scm_service/base"
87
+ require "dpu/scm_service/github"
88
+ require "dpu/scm_service/sourcehut"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dpu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuya.Nishida.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-23 00:00:00.000000000 Z
11
+ date: 2023-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: version_sorter
@@ -81,6 +81,10 @@ files:
81
81
  - exe/dpu
82
82
  - lib/dpu.rb
83
83
  - lib/dpu/cli.rb
84
+ - lib/dpu/scm_service.rb
85
+ - lib/dpu/scm_service/base.rb
86
+ - lib/dpu/scm_service/github.rb
87
+ - lib/dpu/scm_service/sourcehut.rb
84
88
  - lib/dpu/version.rb
85
89
  - sig/dpu.rbs
86
90
  homepage: https://github.com/nishidayuya/dpu