@gradio/code 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @gradio/code
2
2
 
3
+ ## 0.13.0
4
+
5
+ ### Features
6
+
7
+ - [#10812](https://github.com/gradio-app/gradio/pull/10812) [`6384bcc`](https://github.com/gradio-app/gradio/commit/6384bcc11f13d22f4480e7ad7213486fecec8936) - Jedi-based Python code completion on `gr.Code`. Thanks @whitphx!
8
+
9
+ ### Dependency updates
10
+
11
+ - @gradio/atoms@0.14.1
12
+ - @gradio/statustracker@0.10.6
13
+ - @gradio/wasm@0.18.0
14
+ - @gradio/upload@0.15.6
15
+
3
16
  ## 0.12.0
4
17
 
5
18
  ### Features
@@ -9,10 +9,12 @@ import {
9
9
  import { StateEffect, EditorState } from "@codemirror/state";
10
10
  import { indentWithTab } from "@codemirror/commands";
11
11
  import { autocompletion, acceptCompletion } from "@codemirror/autocomplete";
12
+ import { LanguageSupport } from "@codemirror/language";
12
13
  import { basicDark } from "cm6-theme-basic-dark";
13
14
  import { basicLight } from "cm6-theme-basic-light";
14
15
  import { basicSetup } from "./extensions";
15
16
  import { getLanguageExtension } from "./language";
17
+ import { create_pyodide_autocomplete } from "./autocomplete";
16
18
  export let class_names = "";
17
19
  export let value = "";
18
20
  export let dark_mode;
@@ -33,8 +35,14 @@ let element;
33
35
  let view;
34
36
  $:
35
37
  get_lang(language);
38
+ const pyodide_autocomplete = create_pyodide_autocomplete();
36
39
  async function get_lang(val) {
37
40
  const ext = await getLanguageExtension(val);
41
+ if (pyodide_autocomplete && val === "python" && ext instanceof LanguageSupport) {
42
+ ext.support.push(
43
+ ext.language.data.of({ autocomplete: pyodide_autocomplete })
44
+ );
45
+ }
38
46
  lang_extension = ext;
39
47
  }
40
48
  $:
@@ -0,0 +1,4 @@
1
+ import type { CompletionContext, CompletionResult } from "@codemirror/autocomplete";
2
+ type CodeMirrorAutocompleteAsyncFn = (context: CompletionContext) => Promise<CompletionResult | null>;
3
+ export declare function create_pyodide_autocomplete(): CodeMirrorAutocompleteAsyncFn | null;
4
+ export {};
@@ -0,0 +1,49 @@
1
+ import { getWorkerProxyContext } from "@gradio/wasm/svelte";
2
+ // Jedi's completion types to CodeMirror's completion types.
3
+ // If not defined here, Jedi's completion types will be used.
4
+ const completion_type_map = {
5
+ module: "namespace"
6
+ };
7
+ export function create_pyodide_autocomplete() {
8
+ let maybe_worker_proxy;
9
+ try {
10
+ maybe_worker_proxy = getWorkerProxyContext();
11
+ }
12
+ catch (e) {
13
+ console.debug("Not in the Wasm env. Context-aware autocomplete disabled.");
14
+ return null;
15
+ }
16
+ if (!maybe_worker_proxy) {
17
+ return null;
18
+ }
19
+ const worker_proxy = maybe_worker_proxy;
20
+ return async function pyodide_autocomplete(context) {
21
+ try {
22
+ const completions = await worker_proxy.getCodeCompletions({
23
+ code: context.state.doc.toString(),
24
+ line: context.state.doc.lineAt(context.state.selection.main.head)
25
+ .number,
26
+ column: context.state.selection.main.head -
27
+ context.state.doc.lineAt(context.state.selection.main.head).from
28
+ });
29
+ if (completions.length === 0) {
30
+ return null;
31
+ }
32
+ return {
33
+ from: context.state.selection.main.head -
34
+ completions[0].completion_prefix_length,
35
+ options: completions.map((completion) => ({
36
+ label: completion.label,
37
+ type: completion_type_map[completion.type] ?? completion.type,
38
+ documentation: completion.docstring,
39
+ // Items starting with "_" are private attributes and should be sorted last.
40
+ boost: completion.label.startsWith("_") ? -1 : 0
41
+ }))
42
+ };
43
+ }
44
+ catch (e) {
45
+ console.error("Error getting completions", e);
46
+ return null;
47
+ }
48
+ };
49
+ }
@@ -1,23 +1,5 @@
1
1
  import { StreamLanguage } from "@codemirror/language";
2
- import { sql } from "@codemirror/legacy-modes/mode/sql";
3
2
  import { _ } from "svelte-i18n";
4
- const possible_langs = [
5
- "python",
6
- "c",
7
- "cpp",
8
- "markdown",
9
- "json",
10
- "html",
11
- "css",
12
- "javascript",
13
- "jinja2",
14
- "typescript",
15
- "yaml",
16
- "dockerfile",
17
- "shell",
18
- "r",
19
- "sql"
20
- ];
21
3
  const sql_dialects = [
22
4
  "standardSQL",
23
5
  "msSQL",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradio/code",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "description": "Gradio UI packages",
5
5
  "type": "module",
6
6
  "author": "",
@@ -27,12 +27,12 @@
27
27
  "cm6-theme-basic-dark": "^0.2.0",
28
28
  "cm6-theme-basic-light": "^0.2.0",
29
29
  "codemirror": "^6.0.1",
30
- "@gradio/atoms": "^0.14.0",
31
- "@gradio/upload": "^0.15.5",
30
+ "@gradio/atoms": "^0.14.1",
31
+ "@gradio/statustracker": "^0.10.6",
32
32
  "@gradio/icons": "^0.10.0",
33
33
  "@gradio/utils": "^0.10.1",
34
- "@gradio/wasm": "^0.17.4",
35
- "@gradio/statustracker": "^0.10.5"
34
+ "@gradio/upload": "^0.15.6",
35
+ "@gradio/wasm": "^0.18.0"
36
36
  },
37
37
  "main_changeset": true,
38
38
  "main": "./Index.svelte",
@@ -10,11 +10,13 @@
10
10
  import { StateEffect, EditorState, type Extension } from "@codemirror/state";
11
11
  import { indentWithTab } from "@codemirror/commands";
12
12
  import { autocompletion, acceptCompletion } from "@codemirror/autocomplete";
13
+ import { LanguageSupport } from "@codemirror/language";
13
14
 
14
15
  import { basicDark } from "cm6-theme-basic-dark";
15
16
  import { basicLight } from "cm6-theme-basic-light";
16
17
  import { basicSetup } from "./extensions";
17
18
  import { getLanguageExtension } from "./language";
19
+ import { create_pyodide_autocomplete } from "./autocomplete";
18
20
 
19
21
  export let class_names = "";
20
22
  export let value = "";
@@ -42,8 +44,19 @@
42
44
 
43
45
  $: get_lang(language);
44
46
 
47
+ const pyodide_autocomplete = create_pyodide_autocomplete();
48
+
45
49
  async function get_lang(val: string): Promise<void> {
46
50
  const ext = await getLanguageExtension(val);
51
+ if (
52
+ pyodide_autocomplete &&
53
+ val === "python" &&
54
+ ext instanceof LanguageSupport
55
+ ) {
56
+ (ext.support as Extension[]).push(
57
+ ext.language.data.of({ autocomplete: pyodide_autocomplete })
58
+ );
59
+ }
47
60
  lang_extension = ext;
48
61
  }
49
62
 
@@ -0,0 +1,63 @@
1
+ import type {
2
+ CompletionContext,
3
+ CompletionResult
4
+ } from "@codemirror/autocomplete";
5
+ import { getWorkerProxyContext } from "@gradio/wasm/svelte";
6
+ import type { WorkerProxy } from "@gradio/wasm";
7
+
8
+ // Jedi's completion types to CodeMirror's completion types.
9
+ // If not defined here, Jedi's completion types will be used.
10
+ const completion_type_map: Record<string, string> = {
11
+ module: "namespace"
12
+ };
13
+
14
+ type CodeMirrorAutocompleteAsyncFn = (
15
+ context: CompletionContext
16
+ ) => Promise<CompletionResult | null>;
17
+
18
+ export function create_pyodide_autocomplete(): CodeMirrorAutocompleteAsyncFn | null {
19
+ let maybe_worker_proxy: WorkerProxy | undefined;
20
+ try {
21
+ maybe_worker_proxy = getWorkerProxyContext();
22
+ } catch (e) {
23
+ console.debug("Not in the Wasm env. Context-aware autocomplete disabled.");
24
+ return null;
25
+ }
26
+ if (!maybe_worker_proxy) {
27
+ return null;
28
+ }
29
+ const worker_proxy = maybe_worker_proxy;
30
+
31
+ return async function pyodide_autocomplete(
32
+ context: CompletionContext
33
+ ): Promise<CompletionResult | null> {
34
+ try {
35
+ const completions = await worker_proxy.getCodeCompletions({
36
+ code: context.state.doc.toString(),
37
+ line: context.state.doc.lineAt(context.state.selection.main.head)
38
+ .number,
39
+ column:
40
+ context.state.selection.main.head -
41
+ context.state.doc.lineAt(context.state.selection.main.head).from
42
+ });
43
+ if (completions.length === 0) {
44
+ return null;
45
+ }
46
+ return {
47
+ from:
48
+ context.state.selection.main.head -
49
+ completions[0].completion_prefix_length,
50
+ options: completions.map((completion) => ({
51
+ label: completion.label,
52
+ type: completion_type_map[completion.type] ?? completion.type,
53
+ documentation: completion.docstring,
54
+ // Items starting with "_" are private attributes and should be sorted last.
55
+ boost: completion.label.startsWith("_") ? -1 : 0
56
+ }))
57
+ };
58
+ } catch (e) {
59
+ console.error("Error getting completions", e);
60
+ return null;
61
+ }
62
+ };
63
+ }
@@ -1,26 +1,7 @@
1
1
  import type { Extension } from "@codemirror/state";
2
2
  import { StreamLanguage } from "@codemirror/language";
3
- import { sql } from "@codemirror/legacy-modes/mode/sql";
4
3
  import { _ } from "svelte-i18n";
5
4
 
6
- const possible_langs = [
7
- "python",
8
- "c",
9
- "cpp",
10
- "markdown",
11
- "json",
12
- "html",
13
- "css",
14
- "javascript",
15
- "jinja2",
16
- "typescript",
17
- "yaml",
18
- "dockerfile",
19
- "shell",
20
- "r",
21
- "sql"
22
- ];
23
-
24
5
  const sql_dialects = [
25
6
  "standardSQL",
26
7
  "msSQL",