11package org .openactive .gitlab .snippet ;
22
3+ import com .intellij .ide .projectView .ProjectView ;
34import com .intellij .ide .util .PropertiesComponent ;
45import com .intellij .notification .*;
56import com .intellij .openapi .actionSystem .*;
67import com .intellij .openapi .editor .Editor ;
8+ import com .intellij .openapi .fileEditor .FileEditor ;
79import com .intellij .openapi .fileEditor .FileEditorManager ;
10+ import com .intellij .openapi .fileEditor .impl .LoadTextUtil ;
811import com .intellij .openapi .options .Configurable ;
912import com .intellij .openapi .options .ConfigurationException ;
1013import com .intellij .openapi .util .IconLoader ;
1114import com .intellij .openapi .vfs .VirtualFile ;
15+ import com .intellij .psi .PsiElement ;
1216import org .apache .commons .lang .StringUtils ;
1317import org .apache .http .client .methods .CloseableHttpResponse ;
1418import org .apache .http .client .methods .HttpPost ;
19+ import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
1520import org .apache .http .entity .ContentType ;
1621import org .apache .http .entity .StringEntity ;
1722import org .apache .http .impl .client .CloseableHttpClient ;
1823import org .apache .http .impl .client .HttpClients ;
24+ import org .apache .http .ssl .SSLContextBuilder ;
25+ import org .apache .http .ssl .TrustStrategy ;
1926import org .jetbrains .annotations .Nls ;
2027import org .jetbrains .annotations .Nullable ;
2128import org .json .JSONObject ;
2532import java .awt .datatransfer .Clipboard ;
2633import java .awt .datatransfer .StringSelection ;
2734import java .io .InputStream ;
35+ import java .security .cert .CertificateException ;
36+ import java .security .cert .X509Certificate ;
2837import java .util .HashMap ;
2938import java .util .Map ;
3039
@@ -128,16 +137,17 @@ public void apply() throws ConfigurationException
128137 props .setValue ( "org.openactive.gitlab.snippets.token" , tokenField .getText ().trim () );
129138 }
130139
131- private String post ( String text , String fileName ) throws Exception
140+ private String post ( String text , String fileName , VirtualFile [] files ) throws Exception
132141 {
133142 CloseableHttpResponse resp = null ;
134143
135144 HttpPost post = new HttpPost ( getUseableUrl () );
136145 post .addHeader ( "PRIVATE-TOKEN" , getToken () );
137146
138- String data = getData ( fileName , text , fileName );
147+ String data = getData ( fileName , text , fileName , files );
139148 StringEntity ent = new StringEntity ( data , ContentType .APPLICATION_JSON );
140149 post .setEntity ( ent );
150+
141151 try ( CloseableHttpClient client = HttpClients .createDefault () )
142152 {
143153 resp = client .execute ( post );
@@ -163,13 +173,25 @@ private String post( String text, String fileName ) throws Exception
163173 }
164174 }
165175
166- private String getData ( String title , String content , String fileName )
176+ private String getData ( String title , String content , String fileName , VirtualFile [] files )
167177 {
168178 Map <String , String > data = new HashMap <>();
169179 data .put ( "title" , title );
170180 data .put ( "content" , content );
171181 data .put ( "file_name" , fileName );
172182 data .put ( "visibility" , "internal" );
183+
184+ if (files .length > 0 && content == null ) {
185+ data .put ( "title" , "Code Snippet Multiple Files" );
186+ data .put ( "content" , "" );
187+ data .put ( "file_name" , "Code Snippet Multiple Files" );
188+ data .put ( "visibility" , "internal" );
189+ for (VirtualFile file : files ) {
190+ data .put ( "content" ,
191+ data .get ("content" ) + "\n \n --------" + file .getName () +"------------\n \n " +LoadTextUtil .loadText (file ));
192+ }
193+ }
194+
173195 return new JSONObject ( data ).toString ();
174196 }
175197
@@ -178,10 +200,17 @@ public void update( AnActionEvent e )
178200 {
179201 // only show this action if some text is selected
180202 Editor editor = FileEditorManager .getInstance ( e .getProject () ).getSelectedTextEditor ();
203+ VirtualFile [] files = e .getData (CommonDataKeys .VIRTUAL_FILE_ARRAY );
181204 String s = editor .getCaretModel ().getCurrentCaret ().getSelectedText ();
205+
182206 if ( s == null || s .length () == 0 || !isConfigured () )
183207 {
184- e .getPresentation ().setVisible ( false );
208+ e .getPresentation ().setVisible ( false );
209+ }
210+
211+ if (files != null && files .length > 0 )
212+ {
213+ e .getPresentation ().setVisible ( true );
185214 }
186215 }
187216
@@ -193,8 +222,9 @@ public void actionPerformed( AnActionEvent e )
193222 Editor editor = FileEditorManager .getInstance ( e .getProject () ).getSelectedTextEditor ();
194223 VirtualFile vf = e .getData ( PlatformDataKeys .VIRTUAL_FILE );
195224 String s = editor .getCaretModel ().getCurrentCaret ().getSelectedText ();
196- String url = post ( s , vf != null ? vf . getName () : "unknown" );
225+ VirtualFile [] files = e . getData ( CommonDataKeys . VIRTUAL_FILE_ARRAY );
197226
227+ String url = post ( s , vf != null ? vf .getName () : "unknown" , files );
198228
199229 Notification note = new Notification (
200230 "Gitlab Snippet" ,
0 commit comments