From d8ab3dfe0acdd5772c3ad18d396f8506ba75ddd4 Mon Sep 17 00:00:00 2001 From: quiu Date: Fri, 7 Apr 2023 00:43:11 +0300 Subject: [PATCH] add python, java, kotlin langs support --- scripts/data/index.ts | 8 +++- scripts/data/java.ts | 81 ++++++++++++++++++++++++++++++++++++ scripts/data/kotlin.ts | 93 ++++++++++++++++++++++++++++++++++++++++++ scripts/data/python.ts | 87 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 scripts/data/java.ts create mode 100644 scripts/data/kotlin.ts create mode 100644 scripts/data/python.ts diff --git a/scripts/data/index.ts b/scripts/data/index.ts index 77ab9fa5..28f7c472 100644 --- a/scripts/data/index.ts +++ b/scripts/data/index.ts @@ -1,6 +1,9 @@ import { linuxRepo } from "./linux"; import { reactRepo } from "./react"; import { tensorFlowRepo } from "./tensorflow"; +import { kotlinRepo } from "./kotlin"; +import { javaRepo } from "./java"; +import { pythonRepo } from "./python"; export type RepoFile = { path: string; @@ -16,5 +19,8 @@ export type Repo = { export const repoOptions: Repo[] = [ linuxRepo, reactRepo, - tensorFlowRepo + tensorFlowRepo, + kotlinRepo, + javaRepo, + pythonRepo ] diff --git a/scripts/data/java.ts b/scripts/data/java.ts new file mode 100644 index 00000000..d60d1ac8 --- /dev/null +++ b/scripts/data/java.ts @@ -0,0 +1,81 @@ +import { Repo } from "." + +export const javaRepo: Repo = { + label: "Java", + url: "https://github.com/openjdk/jdk", + files: [ + { + path: "/src/java.base/windows/classes/java/lang/Terminator.java", + code:` + package java.lang; + +import jdk.internal.misc.Signal; + +class Terminator { + + private static Signal.Handler handler = null; + + + static void setup() { + if (handler != null) return; + Signal.Handler sh = new Signal.Handler() { + public void handle(Signal sig) { + Shutdown.exit(sig.getNumber() + 0200); + } + }; + handler = sh; + + try { + Signal.handle(new Signal("INT"), sh); + } catch (IllegalArgumentException e) { + } + try { + Signal.handle(new Signal("TERM"), sh); + } catch (IllegalArgumentException e) { + } + } + + static void teardown() { + } + +} + ` + }, + { + path: "/src/jdk.httpserver/windows/classes/sun/net/httpserver/simpleserver/URIPathSegment.java", + code:` + package sun.net.httpserver.simpleserver; + +/** + * A class that represents a URI path segment. + */ +final class URIPathSegment { + + private URIPathSegment() { throw new AssertionError(); } + + /** + * Checks if the segment of a URI path is supported. For example, + * "C:" is supported as a drive on Windows only. + * + * @param segment the segment string + * @return true if the segment is supported + */ + static boolean isSupported(String segment) { + // apply same logic as WindowsPathParser + if (segment.length() >= 2 && isLetter(segment.charAt(0)) && segment.charAt(1) == ':') { + return false; + } + return true; + } + + private static boolean isLetter(char c) { + return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')); + } +} + + ` + }, + + + ] + } diff --git a/scripts/data/kotlin.ts b/scripts/data/kotlin.ts new file mode 100644 index 00000000..ea021772 --- /dev/null +++ b/scripts/data/kotlin.ts @@ -0,0 +1,93 @@ +import { Repo } from "." + + +export const kotlinRepo: Repo = { + label: "Kotlin", + url: "https://github.com/JetBrains/kotlin", + files: + [ + { + path: "/core/util.runtime/src/org/jetbrains/kotlin/utils/collections.kt", + code: ` +package org.jetbrains.kotlin.utils + +import java.util.* + +fun Iterable.keysToMap(value: (K) -> V): Map { + return associateBy({ it }, value) +} + +fun Iterable.keysToMapExceptNulls(value: (K) -> V?): Map { + val map = LinkedHashMap() + for (k in this) { + val v = value(k) + if (v != null) { + map[k] = v + } + } + return map +} + +fun Iterable.mapToIndex(): Map { + val map = LinkedHashMap() + for ((index, k) in this.withIndex()) { + map[k] = index + } + return map +} + +inline fun MutableMap.getOrPutNullable(key: K, defaultValue: () -> V): V { + return if (!containsKey(key)) { + val answer = defaultValue() + put(key, answer) + answer + } else { + @Suppress("UNCHECKED_CAST") + get(key) as V + } +} + +inline fun > C.ifEmpty(body: () -> C): C = if (isEmpty()) body() else this + +inline fun > M.ifEmpty(body: () -> M): M = if (isEmpty()) body() else this + +inline fun Array.ifEmpty(body: () -> Array): Array = if (isEmpty()) body() else this + +fun MutableCollection.addIfNotNull(t: T?) { + if (t != null) add(t) +} + +suspend fun SequenceScope.yieldIfNotNull(t: T?) = if (t != null) yield(t) else Unit + +fun newHashMapWithExpectedSize(expectedSize: Int): HashMap = + HashMap(capacity(expectedSize)) + +fun newHashSetWithExpectedSize(expectedSize: Int): HashSet = + HashSet(capacity(expectedSize)) + +fun newLinkedHashMapWithExpectedSize(expectedSize: Int): LinkedHashMap = + LinkedHashMap(capacity(expectedSize)) + +fun newLinkedHashSetWithExpectedSize(expectedSize: Int): LinkedHashSet = + LinkedHashSet(capacity(expectedSize)) + +private fun capacity(expectedSize: Int): Int = + if (expectedSize < 3) 3 else expectedSize + expectedSize / 3 + 1 + +fun ArrayList.compact(): List = + when (size) { + 0 -> emptyList() + 1 -> listOf(first()) + else -> apply { trimToSize() } + } + +fun List.indexOfFirst(startFrom: Int, predicate: (T) -> Boolean): Int { + for (index in startFrom..lastIndex) { + if (predicate(this[index])) return index + } + return -1 +} + ` + } + ] +} diff --git a/scripts/data/python.ts b/scripts/data/python.ts new file mode 100644 index 00000000..db892370 --- /dev/null +++ b/scripts/data/python.ts @@ -0,0 +1,87 @@ +import { Repo } from "." + +export const pythonRepo: Repo = { + label: "Python", + url: "https://github.com/Python", + files: + [ + { + path: "https://github.com/Pycord-Development/pycord/blob/master/examples/app_commands/slash_cog.py", + code: ` +import discord +from discord.ext import commands + + +class Example(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.slash_command( + guild_ids=[...] + ) # Create a slash command for the supplied guilds. + async def hello(self, ctx: discord.ApplicationContext): + await ctx.respond("Hi, this is a slash command from a cog!") + + @commands.slash_command() # Not passing in guild_ids creates a global slash command. + async def hi(self, ctx: discord.ApplicationContext): + await ctx.respond("Hi, this is a global slash command from a cog!") + + +def setup(bot): + bot.add_cog(Example(bot)) + + ` + }, + { + path: "https://github.com/Pycord-Development/pycord/blob/master/examples/app_commands/info.py", + code: ` + import discord + +intents = discord.Intents.default() +intents.members = True + +bot = discord.Bot( + debug_guilds=[...], + description="An example to showcase how to extract info about users.", + intents=intents, +) + + +@bot.slash_command(name="userinfo", description="Gets info about a user.") +async def info(ctx: discord.ApplicationContext, user: discord.Member = None): + user = ( + user or ctx.author + ) # If no user is provided it'll use the author of the message + embed = discord.Embed( + fields=[ + discord.EmbedField(name="ID", value=str(user.id), inline=False), # User ID + discord.EmbedField( + name="Created", + value=discord.utils.format_dt(user.created_at, "F"), + inline=False, + ), # When the user's account was created + ], + ) + embed.set_author(name=user.name) + embed.set_thumbnail(url=user.display_avatar.url) + + if user.colour.value: # If user has a role with a color + embed.colour = user.colour + + if isinstance(user, discord.User): # Checks if the user in the server + embed.set_footer(text="This user is not in this server.") + else: # We end up here if the user is a discord.Member object + embed.add_field( + name="Joined", + value=discord.utils.format_dt(user.joined_at, "F"), + inline=False, + ) # When the user joined the server + + await ctx.respond(embeds=[embed]) # Sends the embed + + +bot.run("TOKEN") + ` + } + ] +}