1- import { commands , Disposable , events , ExtensionContext , languages , listManager , Position , Range , snippetManager , TextEdit , Uri , window , workspace } from 'coc.nvim'
1+ import { commands , Disposable , events , Document , ExtensionContext , languages , listManager , Position , Range , snippetManager , Uri , window , workspace } from 'coc.nvim'
22import merge from 'merge'
33import path from 'path'
44import { registerLanguageProvider } from './languages'
@@ -7,25 +7,23 @@ import { MassCodeProvider } from './massCodeProvider'
77import { ProviderManager } from './provider'
88import { SnipmateProvider } from './snipmateProvider'
99import { TextmateProvider } from './textmateProvider'
10- import { SnippetEditWithSource , UltiSnippetOption , UltiSnipsConfig } from './types'
10+ import { UltiSnipsConfig } from './types'
1111import { getSnippetsDirectory , UltiSnippetsProvider } from './ultisnipsProvider'
12- import { sameFile , waitDocument } from './util'
12+ import { addFiletypes , getAdditionalFiletype , getSnippetFiletype , insertSnippetEdit , sameFile , waitDocument } from './util'
1313
1414interface API {
1515 expandable : ( ) => Promise < boolean >
1616}
1717
18-
19- async function insertSnippetEdit ( edit : SnippetEditWithSource ) {
20- let ultisnips = edit . source == 'ultisnips' || edit . source == 'snipmate'
21- let option : UltiSnippetOption
22- if ( ultisnips ) {
23- option = {
24- regex : edit . regex ,
25- context : edit . context
26- }
18+ function checkBufferVariable ( doc : Document ) : void {
19+ let filetypes = doc . getVar ( 'snippets_filetypes' , undefined ) as string [ ]
20+ if ( ! Array . isArray ( filetypes ) ) filetypes = undefined
21+ if ( ! filetypes ) {
22+ let arr = getAdditionalFiletype ( doc . bufnr )
23+ if ( arr ) doc . buffer . setVar ( 'coc_snippets_filetypes' , arr , true )
24+ } else if ( filetypes . length > 0 ) {
25+ addFiletypes ( doc . bufnr , filetypes )
2726 }
28- await commands . executeCommand ( 'editor.action.insertSnippet' , TextEdit . replace ( edit . range , edit . newText ) , option )
2927}
3028
3129function enableSnippetsFiletype ( subscriptions : Disposable [ ] ) {
@@ -34,15 +32,17 @@ function enableSnippetsFiletype(subscriptions: Disposable[]) {
3432 if ( doc . uri . endsWith ( '.snippets' ) ) {
3533 doc . buffer . setOption ( 'filetype' , 'snippets' , true )
3634 }
35+ checkBufferVariable ( doc )
3736 } )
3837 workspace . onDidOpenTextDocument ( async document => {
3938 if ( document . uri . endsWith ( '.snippets' ) ) {
4039 let doc = workspace . getDocument ( document . uri )
41- if ( ! doc ) return
42- let { buffer } = doc
43- await buffer . setOption ( 'filetype' , 'snippets' )
40+ let buf = nvim . createBuffer ( doc . bufnr )
41+ buf . setOption ( 'filetype' , 'snippets' , true )
4442 }
43+ checkBufferVariable ( workspace . getDocument ( document . bufnr ) )
4544 } , null , subscriptions )
45+
4646 const rtp = workspace . env . runtimepath
4747 let paths = rtp . split ( ',' )
4848 let idx = paths . findIndex ( s => / ^ u l t i s n i p s $ / i. test ( path . basename ( s ) ) )
@@ -98,6 +98,20 @@ export async function activate(context: ExtensionContext): Promise<API> {
9898 const manager = new ProviderManager ( channel , subscriptions )
9999
100100 enableSnippetsFiletype ( subscriptions )
101+ subscriptions . push ( commands . registerCommand ( 'snippets.addFiletypes' , async ( ...args : string [ ] ) => {
102+ if ( args . length === 0 ) {
103+ let res = await window . requestInput ( 'Filetype to add' , '' , { position : 'center' } )
104+ if ( res == '' ) return
105+ args = res . split ( '.' )
106+ }
107+ let filetypes = args . join ( '.' ) . split ( '.' )
108+ let buf = await nvim . buffer
109+ addFiletypes ( buf . id , filetypes )
110+ let curr = getAdditionalFiletype ( buf . id )
111+ buf . setVar ( 'coc_snippets_filetypes' , curr , true )
112+ manager . loadSnippetsByFiletype ( filetypes . join ( '.' ) )
113+ } ) )
114+
101115 let excludes = configuration . get < string [ ] > ( 'excludePatterns' , [ ] )
102116 if ( ! Array . isArray ( excludes ) ) excludes = [ ]
103117 excludes = excludes . map ( p => workspace . expand ( p ) )
@@ -153,11 +167,8 @@ export async function activate(context: ExtensionContext): Promise<API> {
153167 trace : configuration . get < boolean > ( 'massCode.trace' , false ) ,
154168 excludes
155169 }
156-
157170 let provider = new MassCodeProvider ( channel , config )
158-
159171 manager . regist ( provider , 'massCode' )
160-
161172 subscriptions . push ( commands . registerCommand ( 'snippets.editMassCodeSnippets' , provider . createSnippet . bind ( provider ) ) )
162173 }
163174
@@ -237,7 +248,8 @@ export async function activate(context: ExtensionContext): Promise<API> {
237248 window . showErrorMessage ( 'Document not found' )
238249 return
239250 }
240- let files = await manager . getSnippetFiles ( doc . filetype )
251+ let filetype = getSnippetFiletype ( doc )
252+ let files = await manager . getSnippetFiles ( filetype )
241253 if ( ! files . length ) {
242254 window . showWarningMessage ( 'No related snippet file found' )
243255 return
0 commit comments