diff --git a/Snippet.zip b/GitlabSnippets.zip similarity index 83% rename from Snippet.zip rename to GitlabSnippets.zip index ca4e53c..82b6a86 100644 Binary files a/Snippet.zip and b/GitlabSnippets.zip differ diff --git a/README.md b/README.md index c86d36a..f8d310c 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ # Gitlab Snippets A plugin for Jetbrains IDEs for creating Gitlab snippets within the IDE. +- Support Editor Selection Code Snippet +- Support Multiple Files Selection Snippet ### To Install: -Build or download the Snippet.zip file. Then file -> settings -> Plugins, choose "install plugin from disk". -Select Snippet.zip. Restart your IDE. +Build or download the GitlabSnippets.zip file. Then file -> settings -> Plugins, choose "install plugin from disk". +Select GitlabSnippets.zip. Restart your IDE. ### To Configure: diff --git a/Snippet.iml b/Snippet.iml deleted file mode 100644 index d08f814..0000000 --- a/Snippet.iml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 98fadac..e60eae6 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -25,6 +25,7 @@ + diff --git a/src/org/openactive/gitlab/snippet/SnippetCreate.java b/src/org/openactive/gitlab/snippet/SnippetCreate.java index 832b5c2..161fd08 100644 --- a/src/org/openactive/gitlab/snippet/SnippetCreate.java +++ b/src/org/openactive/gitlab/snippet/SnippetCreate.java @@ -1,21 +1,28 @@ package org.openactive.gitlab.snippet; +import com.intellij.ide.projectView.ProjectView; import com.intellij.ide.util.PropertiesComponent; import com.intellij.notification.*; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.fileEditor.FileEditor; import com.intellij.openapi.fileEditor.FileEditorManager; +import com.intellij.openapi.fileEditor.impl.LoadTextUtil; import com.intellij.openapi.options.Configurable; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.util.IconLoader; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiElement; import org.apache.commons.lang.StringUtils; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContextBuilder; +import org.apache.http.ssl.TrustStrategy; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.Nullable; import org.json.JSONObject; @@ -25,6 +32,8 @@ import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import java.io.InputStream; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.Map; @@ -128,16 +137,17 @@ public void apply() throws ConfigurationException props.setValue( "org.openactive.gitlab.snippets.token", tokenField.getText().trim() ); } - private String post( String text, String fileName ) throws Exception + private String post( String text, String fileName, VirtualFile[] files) throws Exception { CloseableHttpResponse resp = null; HttpPost post = new HttpPost( getUseableUrl() ); post.addHeader( "PRIVATE-TOKEN", getToken() ); - String data = getData( fileName, text, fileName ); + String data = getData( fileName, text, fileName, files ); StringEntity ent = new StringEntity( data, ContentType.APPLICATION_JSON ); post.setEntity( ent ); + try ( CloseableHttpClient client = HttpClients.createDefault() ) { resp = client.execute( post ); @@ -163,13 +173,25 @@ private String post( String text, String fileName ) throws Exception } } - private String getData( String title, String content, String fileName ) + private String getData( String title, String content, String fileName, VirtualFile[] files ) { Map data = new HashMap<>(); data.put( "title", title ); data.put( "content", content ); data.put( "file_name", fileName ); data.put( "visibility", "internal" ); + + if (files.length > 0 && content == null) { + data.put( "title", "Code Snippet Multiple Files" ); + data.put( "content", "" ); + data.put( "file_name", "Code Snippet Multiple Files" ); + data.put( "visibility", "internal" ); + for (VirtualFile file : files) { + data.put( "content", + data.get("content") + "\n\n--------"+ file.getName() +"------------\n\n" +LoadTextUtil.loadText(file)); + } + } + return new JSONObject( data ).toString(); } @@ -178,10 +200,17 @@ public void update( AnActionEvent e ) { // only show this action if some text is selected Editor editor = FileEditorManager.getInstance( e.getProject() ).getSelectedTextEditor(); + VirtualFile[] files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY); String s = editor.getCaretModel().getCurrentCaret().getSelectedText(); + if ( s == null || s.length() == 0 || !isConfigured() ) { - e.getPresentation().setVisible( false ); + e.getPresentation().setVisible( false ); + } + + if (files != null && files.length > 0) + { + e.getPresentation().setVisible( true ); } } @@ -193,8 +222,9 @@ public void actionPerformed( AnActionEvent e ) Editor editor = FileEditorManager.getInstance( e.getProject() ).getSelectedTextEditor(); VirtualFile vf = e.getData( PlatformDataKeys.VIRTUAL_FILE ); String s = editor.getCaretModel().getCurrentCaret().getSelectedText(); - String url = post( s, vf != null ? vf.getName() : "unknown" ); + VirtualFile[] files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY); + String url = post( s, vf != null ? vf.getName() : "unknown", files); Notification note = new Notification( "Gitlab Snippet",