PY-71912: Create gists with the relevant language extensions for the partial selections

GitOrigin-RevId: 56e4b7c9ccdcb56b0b718f675403c86588a4b00f
This commit is contained in:
Ilya Muradyan
2024-09-27 03:55:34 +02:00
committed by intellij-monorepo-bot
parent 5f68405a3d
commit 4486aa89cb
3 changed files with 39 additions and 15 deletions

View File

@@ -18,6 +18,17 @@ import org.jetbrains.plugins.github.util.GithubUtil
import java.io.IOException
open class DefaultGithubGistContentsCollector : GithubGistContentsCollector {
override fun getGistFileName(editor: Editor?, files: Array<VirtualFile>?): String? {
val onlyFile = files?.singleOrNull()?.takeIf { !it.isDirectory }
if (onlyFile != null) {
return onlyFile.name
}
if (editor != null) {
return ""
}
return null
}
override fun collectContents(gistEventData: GithubGistContentsCollector.GistEventData): List<GithubGistRequest.FileContent> {
val (project, editor, file, files) = gistEventData
@@ -41,8 +52,11 @@ open class DefaultGithubGistContentsCollector : GithubGistContentsCollector {
throw IllegalStateException("File, files and editor can't be null all at once!")
}
protected open fun getContentFromEditor(editor: Editor, file: VirtualFile?): List<GithubGistRequest.FileContent>? {
val text: String = ReadAction.compute<String?, java.lang.RuntimeException> { editor.selectionModel.selectedText } ?: editor.document.text
protected open fun getContentFromEditor(
editor: Editor,
file: VirtualFile?,
): List<GithubGistRequest.FileContent>? {
val text: String = getSelectedText(editor) ?: editor.document.text
if (text.isBlank()) {
return null
}
@@ -51,6 +65,9 @@ open class DefaultGithubGistContentsCollector : GithubGistContentsCollector {
return listOf(GithubGistRequest.FileContent(fileName, text))
}
protected fun getSelectedText(editor: Editor): String? =
ReadAction.compute<String?, java.lang.RuntimeException> { editor.selectionModel.selectedText }
private fun getContentFromFile(file: VirtualFile, project: Project, prefix: String?): List<GithubGistRequest.FileContent> {
if (file.isDirectory) {
return getContentFromDirectory(file, project, prefix)

View File

@@ -101,8 +101,9 @@ public class GithubCreateGistAction extends DumbAwareAction {
final VirtualFile @Nullable [] files) {
GithubSettings settings = GithubSettings.getInstance();
// Ask for description and other params
@Nullable String fileName = GithubGistContentsCollector.Companion.getGistFileName(editor, files);
GithubCreateGistDialog dialog = new GithubCreateGistDialog(project,
getFileName(editor, files),
fileName,
settings.isPrivateGist(),
settings.isOpenInBrowserGist(),
settings.isCopyURLGist());
@@ -154,17 +155,6 @@ public class GithubCreateGistAction extends DumbAwareAction {
}.queue();
}
@Nullable
private static String getFileName(@Nullable Editor editor, VirtualFile @Nullable [] files) {
if (files != null && files.length == 1 && !files[0].isDirectory()) {
return files[0].getName();
}
if (editor != null) {
return "";
}
return null;
}
@Nullable
static String createGist(@NotNull Project project,
@NotNull GithubApiRequestExecutor executor,

View File

@@ -25,6 +25,15 @@ interface GithubGistContentsCollector {
*/
fun collectContents(gistEventData: GistEventData): List<GithubGistRequest.FileContent>?
/**
* Retrieves the displayed file name for a gist from the given `editor` and `files` array.
*
* @param editor The editor object which might be null.
* @param files An array of VirtualFile objects which might be null.
* @return The file name as a String if found, otherwise returns null.
*/
fun getGistFileName(editor: Editor?, files: Array<VirtualFile>?): String?
companion object {
val EP = ExtensionPointName.create<GithubGistContentsCollector>("com.intellij.vcs.github.gistContentsCollector")
@@ -35,7 +44,15 @@ interface GithubGistContentsCollector {
files: Array<VirtualFile>?,
): List<GithubGistRequest.FileContent> {
val eventData = GistEventData(project, editor, file, files)
return EP.extensionList.firstNotNullOf { it.collectContents(eventData) }
return EP.extensionList.firstNotNullOf {
it.collectContents(eventData)
}
}
fun getGistFileName(editor: Editor?, files: Array<VirtualFile>?): String? {
return EP.extensionList.firstNotNullOfOrNull {
it.getGistFileName(editor, files)
}
}
}
}