lib/textbringer/commands/dabbrev.rb in textbringer-0.1.4 vs lib/textbringer/commands/dabbrev.rb in textbringer-0.1.5

- old
+ new

@@ -1,57 +1,40 @@ # frozen_string_literal: true module Textbringer - using Module.new { + module DabbrevExtension refine Buffer do - def dabbrev_expand(contd) + def dabbrev_expand(contd = false) if contd && self[:dabbrev_stem] buffers = self[:dabbrev_buffers] buffer = self[:dabbrev_buffer] stem = self[:dabbrev_stem] pos = self[:dabbrev_pos] direction = self[:dabbrev_direction] candidates = self[:dabbrev_candidates] else buffers = Buffer.to_a - buffer = buffers.pop - stem = save_excursion { - pos = point - backward_word(regexp: /[\p{Letter}\p{Number}_\-]/) - substring(point, pos) - } - if stem.empty? - raise "No possible abbreviation" - end + buffers.delete(self) + buffer = self + stem = get_stem_at_point pos = point direction = :backward candidates = [] end - candidates_exclusion = candidates.empty? ? "" : - "(?!(?:" + candidates.map { |s| - Regexp.quote(s) - }.join("|") + ")\\b)" - re = /\b#{Regexp.quote(stem)}#{candidates_exclusion} - ([\p{Letter}\p{Number}_\-]+)/x + re = dabbrev_regexp(stem, candidates) candidate = nil loop do pos, candidate = buffer.dabbrev_search(re, pos, direction) - if pos - break + break if pos + if direction == :backward + pos = buffer.point + direction = :forward else - if direction == :backward - pos = buffer.point - direction = :forward - else - buffer = buffers.pop - if buffer - pos = buffer.point - direction = :backward - else - break - end - end + buffer = buffers.pop + break if buffer.nil? + pos = buffer.point + direction = :backward end end if !candidates.empty? undo end @@ -80,11 +63,35 @@ else nil end end end + + private + + def get_stem_at_point + save_excursion { + pos = point + backward_word(regexp: /[\p{Letter}\p{Number}_\-]/) + if point == pos + raise EditorError, "No possible abbreviation" + end + substring(point, pos) + } + end + + def dabbrev_regexp(stem, candidates) + candidates_exclusion = candidates.empty? ? "" : + "(?!(?:" + candidates.map { |s| + Regexp.quote(s) + }.join("|") + ")\\b)" + /\b#{Regexp.quote(stem)}#{candidates_exclusion} + ([\p{Letter}\p{Number}_\-]+)/x + end end - } + end + + using DabbrevExtension module Commands GLOBAL_MAP.define_key("\e/", :dabbrev_expand_command) define_command(:dabbrev_expand_command) do