-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new command for visual mode to explain the highlighted lines of text. Explanations will be printed to a preview window.
- Loading branch information
Showing
17 changed files
with
578 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
" Author: w0rp <[email protected]> | ||
" Description: Explain a visual selection with Neural. | ||
|
||
let s:current_job = get(s:, 'current_explain_job', 0) | ||
|
||
function! s:AddOutputLine(buffer, job_data, line) abort | ||
call add(a:job_data.output_lines, a:line) | ||
endfunction | ||
|
||
function! s:AddErrorLine(buffer, job_data, line) abort | ||
call add(a:job_data.error_lines, a:line) | ||
endfunction | ||
|
||
function! s:HandleOutputEnd(buffer, job_data, exit_code) abort | ||
" Output an error message from the program if something goes wrong. | ||
if a:exit_code != 0 | ||
" Complain when something goes wrong. | ||
call neural#OutputErrorMessage(join(a:job_data.error_lines, "\n")) | ||
else | ||
let l:i = 0 | ||
|
||
while l:i < len(a:job_data.output_lines) | ||
if !empty(a:job_data.output_lines[l:i]) | ||
break | ||
endif | ||
|
||
let l:i += 1 | ||
endwhile | ||
|
||
call neural#preview#Show( | ||
\ a:job_data.output_lines[l:i :], | ||
\ {'stay_here': 1}, | ||
\) | ||
endif | ||
|
||
let s:current_job = 0 | ||
endfunction | ||
|
||
function! neural#explain#Cleanup() abort | ||
if s:current_job | ||
call neural#job#Stop(s:current_job) | ||
let s:current_job = 0 | ||
endif | ||
endfunction | ||
|
||
function! neural#explain#SelectedLines() abort | ||
" Reload the Neural config if needed. | ||
call neural#config#Load() | ||
" Stop Neural doing anything else if explaining code. | ||
call neural#Cleanup() | ||
|
||
let l:range = neural#visual#GetRange() | ||
let l:buffer = bufnr('') | ||
|
||
let [l:source, l:command] = neural#GetCommand(l:buffer) | ||
|
||
let l:job_data = { | ||
\ 'output_lines': [], | ||
\ 'error_lines': [], | ||
\} | ||
let l:job_id = neural#job#Start(l:command, { | ||
\ 'mode': 'nl', | ||
\ 'out_cb': {job_id, line -> s:AddOutputLine(l:buffer, l:job_data, line)}, | ||
\ 'err_cb': {job_id, line -> s:AddErrorLine(l:buffer, l:job_data, line)}, | ||
\ 'exit_cb': {job_id, exit_code -> s:HandleOutputEnd(l:buffer, l:job_data, exit_code)}, | ||
\}) | ||
|
||
if l:job_id > 0 | ||
let l:lines = neural#redact#PasswordsAndSecrets(l:range.selection) | ||
|
||
let l:config = get(g:neural.source, l:source.name, {}) | ||
|
||
" If the config is not a Dictionary, throw it away. | ||
if type(l:config) isnot v:t_dict | ||
let l:config = {} | ||
endif | ||
|
||
let l:input = { | ||
\ 'config': l:config, | ||
\ 'prompt': "Explain these lines:\n\n" . join(l:lines, "\n"), | ||
\} | ||
call neural#job#SendRaw(l:job_id, json_encode(l:input) . "\n") | ||
else | ||
call neural#OutputErrorMessage('Failed to run ' . l:source.name) | ||
|
||
return | ||
endif | ||
|
||
let s:current_job = l:job_id | ||
endfunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
" Author: w0rp <[email protected]> | ||
" Description: Preview windows for showing whatever information in. | ||
|
||
" Open a preview window and show some lines in it. | ||
" The second argument allows options to be passed in. | ||
" | ||
" filetype - The filetype to use, defaulting to 'neural-preview' | ||
" stay_here - If 1, stay in the window you came from. | ||
function! neural#preview#Show(lines, options) abort | ||
silent pedit NeuralPreviewWindow | ||
wincmd P | ||
|
||
setlocal modifiable | ||
setlocal noreadonly | ||
setlocal nobuflisted | ||
setlocal buftype=nofile | ||
setlocal bufhidden=wipe | ||
:%d | ||
call setline(1, a:lines) | ||
setlocal nomodifiable | ||
setlocal readonly | ||
let &l:filetype = get(a:options, 'filetype', 'neural-preview') | ||
|
||
if get(a:options, 'stay_here') | ||
wincmd p | ||
endif | ||
endfunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
" Author: w0rp <[email protected]> | ||
" Description: Redact passwords and secrets from code. | ||
|
||
" This will never be a perfect implementation, but effort can be made. | ||
" | ||
" The regex to use for keys in objects. | ||
let s:key_regex = 'password|secret[_ ]?key' | ||
" This substitution will be applied for each of the s:key_value_groups. | ||
let s:key_value_sub = '\1\3*\4' | ||
" Regular expressions to replace with s:key_value_sub | ||
let s:key_value_groups = map( | ||
\ [ | ||
\ '("(KEYS)")( *[=:]+ *r?")[^"]+(")', | ||
\ '("(KEYS)")( *[=:]+ *r?'')[^'']+('')', | ||
\ '(''(KEYS)'')( *[=:]+ *r?'')[^'']+('')', | ||
\ '(''(KEYS)'')( *[=:]+ *r?")[^"]+(")', | ||
\ '((KEYS))( *[=:]+ *r?")[^"]+(")', | ||
\ '((KEYS))( *[=:]+ *r?'')[^'']+('')', | ||
\ '((KEYS))( *[=:]+ *r?`)[^`]+(`)', | ||
\ ], | ||
\ {_, template -> '\v\c' . substitute(template, 'KEYS', s:key_regex, '')} | ||
\) | ||
|
||
function! neural#redact#PasswordsAndSecrets(unredacted) abort | ||
let l:line_list = [] | ||
|
||
for l:line in a:unredacted | ||
for l:regex in s:key_value_groups | ||
let l:line = substitute(l:line, l:regex, s:key_value_sub, 'g') | ||
endfor | ||
|
||
call add(l:line_list, l:line) | ||
endfor | ||
|
||
return l:line_list | ||
endfunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
" Author: w0rp <[email protected]> | ||
" Description: Neural functions for working with Vim's visual mode. | ||
|
||
" Get all information for a visual range command we might want. | ||
" | ||
" Credit to xolox for most of this. | ||
function! neural#visual#GetRange() abort | ||
let [l:lnum, l:col] = getpos("'<")[1:2] | ||
let [l:end_lnum, l:end_col] = getpos("'>")[1:2] | ||
let l:end_offset = &selection is# 'inclusive' ? 1 : 2 | ||
|
||
" Get the line range and slice the text we selected. | ||
let l:selection = getline(l:lnum, l:end_lnum) | ||
|
||
if !empty(l:selection) | ||
let l:selection[0] = l:selection[0][l:col - 1:] | ||
let l:selection[-1] = l:selection[-1][: l:end_col - l:end_offset] | ||
" Get the actual end column from the text length, as Vim can give us | ||
" the maximum int for visual line mode. | ||
let l:end_col = len(l:selection[-1]) | ||
endif | ||
|
||
return { | ||
\ 'lnum': l:lnum, | ||
\ 'col': l:col, | ||
\ 'end_lnum': l:end_lnum, | ||
\ 'end_col': l:end_col, | ||
\ 'selection': l:selection, | ||
\} | ||
endfunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.