Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions app/liii/pi/app/interactive/completion-context.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
(define-library (liii pi app interactive completion-context)
(export completion-context completion-needs-quotes?)
(import (scheme base) (scheme char) (liii unicode) (liii string))
(begin
;; Scan code points once; byte indices and terminal cell widths are not columns.
(define (characters s)
(let* ((b (string->utf8 s)) (n (bytevector-length b)))
(let loop
((i 0) (chars '()))
(if (= i n)
(list->vector (reverse chars))
(loop (bytevector-advance-utf8 b i n)
(cons (integer->char (utf8->codepoint-at b i)) chars)
) ;loop
) ;if
) ;let
) ;let*
) ;define
(define (separator? c)
(or (char-whitespace? c)
(memv c
'(#\, #\. #\: #\; #\! #\? #\( #\) #\[ #\] #\{
#\} #\“ #\” #\‘ #\’ #\… #\— #\、 #\。 #\《 #\》
#\〈 #\〉 #\「 #\」 #\『 #\』 #\【 #\】 #\・)
) ;memv
) ;or
) ;define
(define (completion-needs-quotes? value)
(let ((v (characters value)))
(let loop
((i 0))
(and (< i (vector-length v))
(or (and (separator? (vector-ref v i)) #t) (loop (+ i 1)))
) ;and
) ;let
) ;let
) ;define
(define wrappers '((#\( . #\))
(#\[ . #\])
(#\{ . #\})
(#\< . #\>)
(#\` . #\`)))
(define (path-delimiter? c)
(or (separator? c) (memv c '(#\" #\' #\=)))
) ;define
(define (unclosed-quote v n)
(let loop
((i 0) (open #f))
(cond ((= i n) open)
((char=? (vector-ref v i) #\") (loop (+ i 1) (and (not open) i)))
(else (loop (+ i 1) open))
) ;cond
) ;let
) ;define
(define (boundary? v start delimiter?)
(or (= start 0)
(let ((prev (vector-ref v (- start 1))))
(or (delimiter? prev)
(and (assv prev wrappers) (boundary? v (- start 1) delimiter?))
) ;or
) ;let
) ;or
) ;define
(define (token-start v n)
(let loop
((i n))
(if
(or (= i 0) (path-delimiter? (vector-ref v (- i 1))))
i
(loop (- i 1))
) ;if
) ;let
) ;define
(define (skip-wrappers v i n)
(let ((pair (and (< i n) (assv (vector-ref v i) wrappers))))
(if
(and pair
(not
(let loop
((j (+ i 1)))
(and (< j n) (or (char=? (cdr pair) (vector-ref v j)) (loop (+ j 1))))
) ;let
) ;not
) ;and
(skip-wrappers v (+ i 1) n)
i
) ;if
) ;let
) ;define
(define (suffix text i n)
(if (= i n) "" (utf8-substring text i n))
) ;define
(define (path-context text v n)
(let* ((q (unclosed-quote v n))
(at-q
(and q (> q 0) (char=? (vector-ref v (- q 1)) #\@))
) ;at-q
(q-start (if at-q (- q 1) q))
(quoted? (and q (boundary? v q-start (if at-q separator? path-delimiter?)) #t))
(start (if quoted? q-start (skip-wrappers v (token-start v n) n)))
(at? (and (< start n)
(char=? (vector-ref v start) #\@)
(boundary? v start separator?)
) ;and
) ;at?
(query (+ start (if quoted? 1 0) (if at? 1 0)))
) ;
(list (if at? 'at-file 'path)
(suffix text start n)
(suffix text query n)
quoted?
start
) ;list
) ;let*
) ;define
(define (completion-context lines row col reason)
(let* ((text (if (zero? col) "" (utf8-substring (list-ref lines row) 0 col)))
(trimmed (utf8-string-trim-left text))
(slash? (string-starts? trimmed "/"))
(space (string-index trimmed #\space))
(command? (and slash? (not space) (= row 0)))
(tab? (eq? reason 'tab))
(ctx (path-context text (characters text) col))
) ;
(cond ((and tab? command?) (command-context trimmed col))
((and tab? slash? (not (string-index (utf8-string-trim-right trimmed) #\space)))
#f
) ;
((and (not tab?) (string-starts? trimmed "!")) #f)
((eq? (car ctx) 'at-file) ctx)
((and (not tab?) command?) (command-context trimmed col))
((and (not tab?) slash? (= row 0) space)
(let ((arg (substring trimmed (+ space 1) (string-length trimmed))))
(list 'argument arg arg #f (- col (utf8-string-length arg)))
) ;let
) ;
((memq reason '(tab refresh)) ctx)
(else #f)
) ;cond
) ;let*
) ;define
(define (command-context trimmed col)
(list 'command
trimmed
(suffix trimmed 1 (utf8-string-length trimmed))
#f
(- col (utf8-string-length trimmed))
) ;list
) ;define
) ;begin
) ;define-library
49 changes: 49 additions & 0 deletions app/liii/pi/app/interactive/completion-edit.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(define-library (liii pi app interactive completion-edit)
(export completion-edit-plan apply-completion-edit!)
(import (scheme base)
(liii tui editor component)
(liii unicode)
(liii string)
(liii pi app interactive completion-context)
) ;import
(begin
(define (apply-completion-edit! editor plan)
(apply editor-replace-range! editor plan)
) ;define

(define (completion-edit-plan lines row col context value directory?)
(let* ((kind (car context))
(start (list-ref context 4))
(line (list-ref lines row))
(path? (memq kind '(path at-file)))
(quoted? (and path? (or (list-ref context 3) (completion-needs-quotes? value))))
(dir-suffix
(if (and directory? (not (string-ends? value "/"))) "/" "")
) ;dir-suffix
(quote-mark (if quoted? "\"" ""))
(text
(cond ((eq? kind 'command) (string-append "/" value " "))
((eq? kind 'argument) value)
(else
(string-append (if (eq? kind 'at-file) "@" "")
quote-mark
value
dir-suffix
quote-mark
(if (and (eq? kind 'at-file) (not directory?)) " " "")
) ;string-append
) ;else
) ;cond
) ;text
(consume? (and quoted?
(< col (utf8-string-length line))
(char=? (utf8-string-ref line col) #\")
) ;and
) ;consume?
(offset (- (utf8-string-length text) (if (and quoted? directory?) 1 0)))
) ;
(list row start (if consume? (+ col 1) col) text offset)
) ;let*
) ;define
) ;begin
) ;define-library
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# (liii pi app interactive completion-context)

- `completion-context` — 按逻辑行、码点列和触发原因返回补全类型及替换前缀,不查询候选。
- `completion-needs-quotes?` — 判断路径值是否含有会分开 token 的空白或中文标点。
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
;; completion-context
;; 识别光标前的补全请求。
;;
;; 语法
;; ----
;; (completion-context lines row col reason)
;;
;; 参数
;; ----
;; lines : list? 逻辑行。
;; row : integer? 行号。
;; col : integer? 码点列。
;; reason : symbol? natural、tab 或 refresh。
;;
;; 返回值
;; ----
;; list? 或 #f;依次为类型、原前缀、查询、引号标志和起点。

(import (scheme base)
(liii check)
(liii unicode)
(liii pi app interactive completion-context)
) ;import
(check-set-mode! 'report-failed)
(for-each
(lambda (c)
(let* ((line (car c))
(row (cadr c))
(lines (if (= row 0) (list line) (list "/mo" line)))
) ;
(check (completion-context lines row (utf8-string-length line) (caddr c))
=>
(list-ref c 3)
) ;check
) ;let*
) ;lambda
'(("/mo" 0 natural (command "/mo" "mo" #f 0))
(" /zzz" 0 tab (command "/zzz" "zzz" #f 2))
("/tmp/fi" 0 tab (command "/tmp/fi" "tmp/fi" #f 0))
("/tmp/fi" 1 tab #f)
("请看 /tmp/fi" 1 tab (path "/tmp/fi" "/tmp/fi" #f 3))
("/model " 0 tab #f)
("/model src" 0 tab (path "src" "src" #f 7))
("/model " 0 natural (argument "" "" #f 7))
("/cmd @edi" 0 natural (at-file "@edi" "edi" #f 5))
("请看:@设计" 0 natural (at-file "@设计" "设计" #f 3))
("foo@example.com" 0 natural #f)
("请看@设计" 0 natural #f)
("key=@foo" 0 natural #f)
("key=src" 0 tab (path "src" "src" #f 4))
("看 (@\"有 空" 0 natural (at-file "@\"有 空" "有 空" #t 3))
("word(@foo" 0 natural #f)
("app/[slug]/" 0 refresh (path "app/[slug]/" "app/[slug]/" #f 0))
("! @foo" 0 natural #f)
("src" 0 natural #f)
("" 0 tab (path "" "" #f 0))
("/" 0 tab (command "/" "" #f 0))
("/中文 参数" 0 refresh (argument "参数" "参数" #f 4))
(" " 0 tab (path "" "" #f 1)))
) ;for-each

(check-report)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
;; completion-needs-quotes?
;; 判断路径值在补全文本中是否需要引号。
;;
;; 语法
;; ----
;; (completion-needs-quotes? value)
;;
;; 参数
;; ----
;; value : string? 原始路径。
;;
;; 返回值
;; ----
;; boolean? 空白或中文分隔标点需要引号。

(import (scheme base) (liii check) (liii pi app interactive completion-context))
(check-set-mode! 'report-failed)
(check (completion-needs-quotes? "有 空格") => #t)
(check (completion-needs-quotes? "看:图") => #t)
(check (completion-needs-quotes? "目录/[slug]/") => #f)

(check-report)
4 changes: 4 additions & 0 deletions app/tests/liii/pi/app/interactive/completion-edit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# (liii pi app interactive completion-edit)

- `completion-edit-plan` — 从上下文和未装饰的候选值生成单行替换计划,保持右侧文字。
- `apply-completion-edit!` — 用 Goldfish `editor-replace-range!` 原子应用计划。最低已验证的 Goldfish 修订为 `b045280c7`;原版 18.11.72 不支持,不回退为整框重写。
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
;; apply-completion-edit!
;; 将计划写回编辑器。
;;
;; 语法
;; ----
;; (apply-completion-edit! editor plan)
;;
;; 参数
;; ----
;; editor : editor? 编辑组件。
;; plan : list? completion-edit-plan 的返回值。
;;
;; 返回值
;; ----
;; #t;由 Goldfish 的单行原子编辑维护光标、粘贴和撤销。

(import (scheme base)
(liii check)
(liii tui)
(liii pi app interactive completion-context)
(liii pi app interactive completion-edit)
) ;import
(check-set-mode! 'report-failed)
(let* ((ed (make-editor "前行\n请 @\"设\"右侧\n后行" :multiline #t))
(lines (editor-lines ed))
(ctx (completion-context lines 1 5 'natural))
) ;
(editor-set-cursor! ed 1 5)
(apply-completion-edit! ed
(completion-edit-plan lines 1 5 ctx "设计 文档" #t)
) ;apply-completion-edit!
(check (editor-lines ed)
=>
'("前行" "请 @\"设计 文档/\"右侧" "后行")
) ;check
(check (editor-cursor ed) => '(1 10))
(component-handle-input ed 'ctrl-z)
(check (editor-lines ed) => lines)
(check (editor-cursor ed) => '(1 5))
) ;let*

(check-report)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
;; completion-edit-plan
;; 根据候选构造单行编辑。
;;
;; 语法
;; ----
;; (completion-edit-plan lines row col context value directory?)
;;
;; 参数
;; ----
;; lines : list? 逻辑行。
;; row : integer? 行号。
;; col : integer? 码点列。
;; context : list? 补全上下文。
;; value : string? 候选值,不含补全添加的引号或 @。
;; directory? : boolean? 是否为目录。
;;
;; 返回值
;; ----
;; list? 行号、起点、排他终点、替换文本、光标相对偏移。

(import (scheme base)
(liii check)
(liii pi app interactive completion-context)
(liii pi app interactive completion-edit)
) ;import
(check-set-mode! 'report-failed)

(define (plan line col reason value dir?)
(let ((ctx (completion-context (list line) 0 col reason)))
(completion-edit-plan (list line) 0 col ctx value dir?)
) ;let
) ;define
(check (plan "请 @\"设\"右侧" 5 'natural "设计 文档" #t)
=>
'(0 2 6 "@\"设计 文档/\"" 8)
) ;check
(check (plan "@设后" 2 'natural "设计 文档" #f)
=>
'(0 0 2 "@\"设计 文档\" " 9)
) ;check
(check (plan "看 src/ma后" 8 'tab "src/main" #f) => '(0 2 8 "src/main" 8))
(check (plan "/mo" 3 'tab "model" #f) => '(0 0 3 "/model " 7))

(check-report)
Loading