Skip to content

fix(Save-/LoadMarks): add opt to keep original file-ext #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

juanMarinero
Copy link
Contributor

  • Previous let l:path = substitute(l:path, expand("%:e"), "json", "") did replace first extension match. E.g. filepy.py would be filejson.py
  • If I have multiple files with same filename (without extension), like main.js and main.html, then now with let g:codepainter_file_keep_original_extension = 1 I can have 2 different codepainter JSONs (main.js.json and main.html.json). Or if set to 0 just main.json as previous to this PR
  • Previous potential overwrite if script with marks is also a JSON. Now codepainter#SaveMarks() echoes a warning, thus silent removed from PainterSaveMarks declaration to see those error-msgs

Next is an example of what now can be achieved. TLDR: create JSON files for auto-save/load codepainter-marks with a regex that matches .gitignore. Example of how to use:

  1. vim filepy.py
  2. [Create a codepainter mark]
  3. :call SaveCodePaintersToJSON() creates filepy.py__noGit_vim-codePainters.json. Which is awesome since py of filepyis not renamed by let l:path = substitute(l:path, expand("%:e"), "json", "") and adds _noGit_ for the .gitignore
  4. Exit vim
  5. vim filepy.py
  6. :call SourceCodePaintersFromJSON() loads filepy.py__noGit_vim-codePainters.json
" Source marks from JSON
nmap <leader>mmso :call SourceCodePaintersFromJSON()<CR>

" Save marks to JSON
nmap <leader>mmsa :call SaveCodePaintersToJSON()<CR>

function! SetCodePaintersFile()
  " Check if g:codepaints_file_default_suffix is defined, and set a default if not
  if !exists('g:codepaints_file_default_suffix')
    let g:codepaints_file_default_suffix = '__noGit_vim-codePainters.json'
  endif

  " Full path
  " Keep file extension, to prevent overwriting codePainters-associated files with the same name. E.g. of main.js and main.html
  " Not valid cause 'PainterSaveMarks' rewrites the first ext to 'json' 
  " even if e.g. ext is in part of the file name
  " e.g. file_py.py is renamed to file_json.py
  " See codepainter#LoadMarks(...) and codepainter#SaveMarks(...) in
  " autoload/codepainter.vim
  " Yes valid since current PR !!!!!!!!!!!!
  let l:current_file = expand('%:p')

  " Add suffix
  let l:codepaints_file = l:current_file . g:codepaints_file_default_suffix

  " Return the code_painter file path
  return l:codepaints_file
endfunction

function! SaveCodePaintersToJSON()
  " Call SetCodePaintersFile to set up the bookmark file path
  let l:codepaints_file = SetCodePaintersFile()

  " Check if file already exists
  if filereadable(l:codepaints_file)
    let l:choice = input('Overwrite ' . l:codepaints_file . '? (y/n): ')
    if l:choice !~? '^y'
      echo 'CodePaints save cancelled'
      return
    endif
  endif

  execute 'PainterSaveMarks ' . l:codepaints_file
  echo 'Code paints saved to ' . l:codepaints_file
endfunction

function! SourceCodePaintersFromJSON()
  " Call SetCodePaintersFile to set up the bookmark file path
  let l:codepaints_file = SetCodePaintersFile()

  if !filereadable(l:codepaints_file)
    echo 'Code paints file not found: ' . l:codepaints_file
    return
  endif

  execute 'PainterLoadMarks ' . l:codepaints_file
  echo 'Code paints sourced from ' . l:codepaints_file
endfunction

If no arg passed then l:aux is still expand("%:e")
otherwise extension depends on arg passed
g:codepainter_file_keep_original_extension to keep (1) or not (0)
original file extension in JSON file to store markers.
Also:
- added error-msg if file to store markers is current JSON
- 'silent' removed from PainterSaveMarks to see those error-msgs
@juanMarinero
Copy link
Contributor Author

Amend. I said

create JSON files for auto-save/load codepainter-marks

Though to be auto it would need alike:

au BufReadPost * if empty(&buftype) | call SourceCodePaintersFromJSON() | endif
au BufUnload * if empty(&buftype) | call SaveCodePaintersToJSON() | endif
  • BufReadPost seems ok. It is convenient: it loads the associated codepainter JSON (if already exists) on file open. I think it runs on regular scripts only (for example to not be triggered when git-status, :Git, of vim-fugitive loads).
  • BufUnload is NOT ok. It also only runs on regular scripts. To be triggered on respective-buffer-unload I edited:
au BufUnload * if empty(&buftype) | call SaveCodePaintersToJSON(expand('<afile>:p')) | endif

function! SetCodePaintersFile(...)
  [...] 
  
  " replaced next: let l:current_file = expand('%:p')
  let l:current_file = a:0 > 0 ? a:1 : expand('%:p')
  
  [...]
endfunction

function! SaveCodePaintersToJSON(...)
  [...]
  
  " replaced next: let l:codepaints_file = SetCodePaintersFile()
  let l:codepaints_file = a:0 == 0 ? SetCodePaintersFile() : SetCodePaintersFile(a:1)  
  
  [...]
endfunction

But BufUnload is not working as desired. I think that codepainter#SaveMarks is saving marks from all buffers into each file, rather than just saving the marks specific to each buffer.
Example

  1. vim file1.py which has its respective codepainter-marks JSON created
  2. :echo g:marks shows e.g. {'1': [[[0, 1, 1, 0], [0, 1, 5, 0], 0, 'paint0']]}
  3. :q
  4. vim -p file1.py A.json filepy.py where all they already had their respective codepainter-marks JSON created
  5. :echo g:marks shows e.g. {'1': [[[0, 1, 1, 0], [0, 1, 5, 0], 0, 'paint0'], [[0, 1, 1, 0], [0, 1, 2, 0], 1, 'paint0']], '2': [[[0, 2, 3, 0], [0, 2, 7, 0], 2, 'paint0']], '3': [[[0, 3, 13, 0], [0, 3, 20, 0], 3, 'paint0' ], [[0, 3, 1, 0], [0, 3, 3, 0], 4, 'paint0']], '5': [[[0, 5, 1, 0], [0, 5, 6, 0], 5, 'paint3']]} instead of just marks of first buffer file1.py.

Alike happens with the BufUnload command which calls SaveCodePaintersToJSON and this calls codepainter#SaveMarks.
Example

  1. vim -p file1.py A.json filepy.py. Where all they already had their respective codepainter-marks JSON created.
  2. If I :q file1.py and filepy.py nothing happens. Then when I quit A.json I am asked:
    Overwrite ~/Downloads/deleteme/file1.py__noGit_vim-codePainters.json? (y/n): y
    Overwrite ~/Downloads/deleteme/A.json__noGit_vim-codePainters.json? (y/n): n
    Overwrite ~/Downloads/deleteme/filepy.py__noGit_vim-codePainters.json? (y/n): n
  1. I answered y save just file1.py__noGit_vim-codePainters.json
  2. But in that file I get the mix of all the 3 JSONs (file1.py__noGit_vim-codePainters.json, A.json__noGit_vim-codePainters.json and filepy.py__noGit_vim-codePainters.json) as vim file1.py can show.

Please help. To fix codepainter#SaveMarks saving marks from all buffers rather than just saving the marks specific to each buffer.

Maybe vim-bookmarks approach can be replicated here (way beyond my vimscript skills though). This plugin offers different strategies to save bookmarks:

  • Global (in ~/.vim-bookmarks by default)
  • Per working directory
  • Per buffer
  • Or none of the above, like manual. It's called Silent saving and loading

Maybe all this auto-stuff goes beyond this PR and if this PR is accepted then I should create an issue (feature request).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant