From 823bad63384de90c11653f0afa253cc5d0e19239 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 4 May 2022 11:27:42 +0000 Subject: Rename completion fields --- test/console/components/console/Completion.test.tsx | 14 +++++++------- test/console/components/console/CompletionItem.test.tsx | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'test') diff --git a/test/console/components/console/Completion.test.tsx b/test/console/components/console/Completion.test.tsx index 7dd634f..dee0e80 100644 --- a/test/console/components/console/Completion.test.tsx +++ b/test/console/components/console/Completion.test.tsx @@ -9,17 +9,17 @@ describe("console/components/console/completion/Completion", () => { { name: "Fruit", items: [ - { caption: "apple" }, - { caption: "banana" }, - { caption: "cherry" }, + { primary: "apple" }, + { primary: "banana" }, + { primary: "cherry" }, ], }, { name: "Element", items: [ - { caption: "argon" }, - { caption: "boron" }, - { caption: "carbon" }, + { primary: "argon" }, + { primary: "boron" }, + { primary: "carbon" }, ], }, ]; @@ -39,7 +39,7 @@ describe("console/components/console/completion/Completion", () => { const items = group.findAllByType(CompletionItem); expect(items).toHaveLength(completions[i].items.length); items.forEach((item, j) => { - expect(item.props.caption).toEqual(completions[i].items[j].caption); + expect(item.props.primary).toEqual(completions[i].items[j].primary); }); }); }); diff --git a/test/console/components/console/CompletionItem.test.tsx b/test/console/components/console/CompletionItem.test.tsx index ae73b21..6e89bc8 100644 --- a/test/console/components/console/CompletionItem.test.tsx +++ b/test/console/components/console/CompletionItem.test.tsx @@ -8,8 +8,8 @@ describe("console/components/console/completion/CompletionItem", () => { ).root; const spans = root.findAllByType("span"); -- cgit v1.2.3 From bc890c55bfbd6d1aa4ece5b0ef96a9ab6d358ad7 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 14 Mar 2022 13:24:01 +0000 Subject: Get completion types on component initialization --- src/console/completion/actions.ts | 16 ---------------- src/console/completion/hooks.ts | 24 ++++++++---------------- src/console/completion/hooks/clients.ts | 23 +++++++++++++++++++++++ src/console/completion/reducer.ts | 8 -------- test/console/completion/reducer.test.ts | 16 ---------------- 5 files changed, 31 insertions(+), 56 deletions(-) create mode 100644 src/console/completion/hooks/clients.ts (limited to 'test') diff --git a/src/console/completion/actions.ts b/src/console/completion/actions.ts index 59d1a04..0c5e1f1 100644 --- a/src/console/completion/actions.ts +++ b/src/console/completion/actions.ts @@ -1,17 +1,10 @@ -import CompletionType from "../../shared/CompletionType"; import Completions from "../Completions"; -export const INIT_COMPLETIONS = "reset.completions"; export const SET_COMPLETION_SOURCE = "set.completion.source"; export const SET_COMPLETIONS = "set.completions"; export const COMPLETION_NEXT = "completion.next"; export const COMPLETION_PREV = "completion.prev"; -export interface InitCompletionAction { - type: typeof INIT_COMPLETIONS; - completionTypes: CompletionType[]; -} - export interface SetCompletionSourceAction { type: typeof SET_COMPLETION_SOURCE; completionSource: string; @@ -31,20 +24,11 @@ export interface CompletionPrevAction { } export type CompletionAction = - | InitCompletionAction | SetCompletionSourceAction | SetCompletionsAction | CompletionNextAction | CompletionPrevAction; -export const initCompletion = ( - completionTypes: CompletionType[] -): InitCompletionAction => { - return { - type: INIT_COMPLETIONS, - completionTypes, - }; -}; export const setCompletionSource = ( query: string ): SetCompletionSourceAction => { diff --git a/src/console/completion/hooks.ts b/src/console/completion/hooks.ts index ad315e6..62baab4 100644 --- a/src/console/completion/hooks.ts +++ b/src/console/completion/hooks.ts @@ -11,6 +11,7 @@ import CommandLineParser, { import { UnknownCommandError } from "../commandline/CommandParser"; import Completions from "../Completions"; import CompletionType from "../../shared/CompletionType"; +import { useGetCompletionTypes } from "./hooks/clients"; const commandDocs = { [Command.Set]: "Set a value of the property", @@ -208,21 +209,15 @@ export const useCompletions = () => { const state = React.useContext(CompletionStateContext); const dispatch = React.useContext(CompletionDispatchContext); const commandLineParser = React.useMemo(() => new CommandLineParser(), []); + const [completionTypes] = useGetCompletionTypes(); const updateCompletions = React.useCallback((source: string) => { dispatch(actions.setCompletionSource(source)); }, []); - const initCompletion = React.useCallback((source: string) => { - completionClient.getCompletionTypes().then((completionTypes) => { - dispatch(actions.initCompletion(completionTypes)); - dispatch(actions.setCompletionSource(source)); - }); - }, []); - const { delayedCallback: queryCompletions, enableDelay } = useDelayedCallback( React.useCallback( - (text: string, completionTypes?: CompletionType[]) => { + (text: string, completionTypes: CompletionType[]) => { const phase = commandLineParser.inputPhase(text); if (phase === InputPhase.OnCommand) { getCommandCompletions(text).then((completions) => @@ -241,11 +236,6 @@ export const useCompletions = () => { case Command.Open: case Command.TabOpen: case Command.WindowOpen: - if (!completionTypes) { - initCompletion(text); - return; - } - getOpenCompletions(cmd.command, cmd.args, completionTypes).then( (completions) => dispatch(actions.setCompletions(completions)) ); @@ -282,13 +272,15 @@ export const useCompletions = () => { ); React.useEffect(() => { - queryCompletions(state.completionSource, state.completionTypes); - }, [state.completionSource, state.completionTypes]); + if (typeof completionTypes === "undefined") { + return; + } + queryCompletions(state.completionSource, completionTypes); + }, [state.completionSource, completionTypes]); return { completions: state.completions, updateCompletions, - initCompletion, }; }; diff --git a/src/console/completion/hooks/clients.ts b/src/console/completion/hooks/clients.ts new file mode 100644 index 0000000..49deb0c --- /dev/null +++ b/src/console/completion/hooks/clients.ts @@ -0,0 +1,23 @@ +import React from "react"; +import CompletionClient from "../../clients/CompletionClient"; +import CompletionType from "../../../shared/CompletionType"; + +const completionClient = new CompletionClient(); + +export const useGetCompletionTypes = (): [ + CompletionType[] | undefined, + boolean +] => { + type State = { + loading: boolean; + result?: CompletionType[]; + }; + const [state, setState] = React.useState({ loading: true }); + + React.useEffect(() => { + completionClient.getCompletionTypes().then((result) => { + setState({ loading: false, result }); + }); + }, []); + return [state.result, state.loading]; +}; diff --git a/src/console/completion/reducer.ts b/src/console/completion/reducer.ts index 905451f..0b34114 100644 --- a/src/console/completion/reducer.ts +++ b/src/console/completion/reducer.ts @@ -1,7 +1,6 @@ import Completions from "../Completions"; import CompletionType from "../../shared/CompletionType"; import { - INIT_COMPLETIONS, SET_COMPLETION_SOURCE, SET_COMPLETIONS, COMPLETION_NEXT, @@ -58,13 +57,6 @@ export default function reducer( action: CompletionAction ): State { switch (action.type) { - case INIT_COMPLETIONS: - return { - ...state, - completionTypes: action.completionTypes, - completions: [], - select: -1, - }; case SET_COMPLETION_SOURCE: return { ...state, diff --git a/test/console/completion/reducer.test.ts b/test/console/completion/reducer.test.ts index 43b9807..865d066 100644 --- a/test/console/completion/reducer.test.ts +++ b/test/console/completion/reducer.test.ts @@ -3,29 +3,13 @@ import reducer, { State, } from "../../../src/console/completion/reducer"; import { - initCompletion, selectNext, selectPrev, setCompletions, setCompletionSource, } from "../../../src/console/completion/actions"; -import CompletionType from "../../../src/shared/CompletionType"; describe("completion reducer", () => { - describe("initCompletion", () => { - it("initializes completions", () => { - const nextState = reducer( - defaultState, - initCompletion([CompletionType.Bookmarks, CompletionType.History]) - ); - - expect(nextState.completionTypes).toEqual([ - CompletionType.Bookmarks, - CompletionType.History, - ]); - }); - }); - describe("setCompletionSource", () => { it("sets a completion source", () => { const nextState = reducer(defaultState, setCompletionSource("open ")); -- cgit v1.2.3