Skip to content

Commit 254c050

Browse files
committed
add multiple file selection support for generating snippet
1 parent cfb3bc1 commit 254c050

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed
Binary file not shown.

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Gitlab Snippets
22

33
A plugin for Jetbrains IDEs for creating Gitlab snippets within the IDE.
4+
- Support Editor Selection Code Snippet
5+
- Support Multiple Files Selection Snippet
46

57
### To Install:
68

7-
Build or download the Snippet.zip file. Then file -> settings -> Plugins, choose "install plugin from disk".
8-
Select Snippet.zip. Restart your IDE.
9+
Build or download the GitlabSnippets.zip file. Then file -> settings -> Plugins, choose "install plugin from disk".
10+
Select GitlabSnippets.zip. Restart your IDE.
911

1012

1113
### To Configure:

Snippet.iml

Lines changed: 0 additions & 22 deletions
This file was deleted.

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<action id="Snippet.Create" class="org.openactive.gitlab.snippet.SnippetCreate"
2626
text="Gitlab Snippet" description="Create a Snippet">
2727
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
28+
<add-to-group group-id="ProjectViewPopupMenu" anchor="last"/>
2829
</action>
2930
</actions>
3031

src/org/openactive/gitlab/snippet/SnippetCreate.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
package org.openactive.gitlab.snippet;
22

3+
import com.intellij.ide.projectView.ProjectView;
34
import com.intellij.ide.util.PropertiesComponent;
45
import com.intellij.notification.*;
56
import com.intellij.openapi.actionSystem.*;
67
import com.intellij.openapi.editor.Editor;
8+
import com.intellij.openapi.fileEditor.FileEditor;
79
import com.intellij.openapi.fileEditor.FileEditorManager;
10+
import com.intellij.openapi.fileEditor.impl.LoadTextUtil;
811
import com.intellij.openapi.options.Configurable;
912
import com.intellij.openapi.options.ConfigurationException;
1013
import com.intellij.openapi.util.IconLoader;
1114
import com.intellij.openapi.vfs.VirtualFile;
15+
import com.intellij.psi.PsiElement;
1216
import org.apache.commons.lang.StringUtils;
1317
import org.apache.http.client.methods.CloseableHttpResponse;
1418
import org.apache.http.client.methods.HttpPost;
19+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
1520
import org.apache.http.entity.ContentType;
1621
import org.apache.http.entity.StringEntity;
1722
import org.apache.http.impl.client.CloseableHttpClient;
1823
import org.apache.http.impl.client.HttpClients;
24+
import org.apache.http.ssl.SSLContextBuilder;
25+
import org.apache.http.ssl.TrustStrategy;
1926
import org.jetbrains.annotations.Nls;
2027
import org.jetbrains.annotations.Nullable;
2128
import org.json.JSONObject;
@@ -25,6 +32,8 @@
2532
import java.awt.datatransfer.Clipboard;
2633
import java.awt.datatransfer.StringSelection;
2734
import java.io.InputStream;
35+
import java.security.cert.CertificateException;
36+
import java.security.cert.X509Certificate;
2837
import java.util.HashMap;
2938
import 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

Comments
 (0)