diff options
| author | Nate Sesti <sestinj@gmail.com> | 2023-07-16 16:25:02 -0700 | 
|---|---|---|
| committer | Nate Sesti <sestinj@gmail.com> | 2023-07-16 16:25:02 -0700 | 
| commit | da7827189181328baa329d2984d0fd9c6d476be3 (patch) | |
| tree | 56308a5595e8e01ab83974688d8ac8e4945f319a /extension/react-app/src/util | |
| parent | d80119982e9b60ca0022533a0086eb526dc7d957 (diff) | |
| parent | eab69781a3e3b5236916d9057ce29aba2e868913 (diff) | |
| download | sncontinue-da7827189181328baa329d2984d0fd9c6d476be3.tar.gz sncontinue-da7827189181328baa329d2984d0fd9c6d476be3.tar.bz2 sncontinue-da7827189181328baa329d2984d0fd9c6d476be3.zip | |
Merge branch 'main' into ggml-server
Diffstat (limited to 'extension/react-app/src/util')
| -rw-r--r-- | extension/react-app/src/util/api.ts | 43 | ||||
| -rw-r--r-- | extension/react-app/src/util/editCache.ts | 89 | ||||
| -rw-r--r-- | extension/react-app/src/util/index.ts | 62 | 
3 files changed, 39 insertions, 155 deletions
| diff --git a/extension/react-app/src/util/api.ts b/extension/react-app/src/util/api.ts deleted file mode 100644 index bdec1d20..00000000 --- a/extension/react-app/src/util/api.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { -  Configuration, -  DebugApi, -  UnittestApi, -  ChatApi, -} from "../../../src/client"; -import { useSelector } from "react-redux"; -import { useEffect, useState } from "react"; -import { RootStore } from "../redux/store"; - -export function useApi() { -  const apiUrl = useSelector((state: RootStore) => state.config.apiUrl); -  const vscMachineId = useSelector( -    (state: RootStore) => state.config.vscMachineId -  ); -  const [debugApi, setDebugApi] = useState<DebugApi>(); -  const [unittestApi, setUnittestApi] = useState<UnittestApi>(); -  const [chatApi, setChatApi] = useState<ChatApi>(); - -  useEffect(() => { -    if (apiUrl && vscMachineId) { -      let config = new Configuration({ -        basePath: apiUrl, -        fetchApi: fetch, -        middleware: [ -          { -            pre: async (context) => { -              context.init.headers = { -                ...context.init.headers, -                "x-vsc-machine-id": vscMachineId, -              }; -            }, -          }, -        ], -      }); -      setDebugApi(new DebugApi(config)); -      setUnittestApi(new UnittestApi(config)); -      setChatApi(new ChatApi(config)); -    } -  }, [apiUrl, vscMachineId]); - -  return { debugApi, unittestApi, chatApi }; -} diff --git a/extension/react-app/src/util/editCache.ts b/extension/react-app/src/util/editCache.ts deleted file mode 100644 index b8071127..00000000 --- a/extension/react-app/src/util/editCache.ts +++ /dev/null @@ -1,89 +0,0 @@ -import { useApi } from "../util/api"; -import { FileEdit, SerializedDebugContext } from "../../../src/client"; -import { useCallback, useEffect, useState } from "react"; - -export function useEditCache() { -  const { debugApi } = useApi(); - -  const fetchNewEdit = useCallback( -    async (debugContext: SerializedDebugContext) => { -      return ( -        await debugApi?.editEndpointDebugEditPost({ -          serializedDebugContext: debugContext, -        }) -      )?.completion; -    }, -    [debugApi] -  ); - -  const [editCache, setEditCache] = useState(new EditCache(fetchNewEdit)); - -  useEffect(() => { -    setEditCache(new EditCache(fetchNewEdit)); -  }, [fetchNewEdit]); - -  return editCache; -} - -/** - * Stores preloaded edits, invalidating based off of debug context changes - */ -class EditCache { -  private _lastDebugContext: SerializedDebugContext | undefined; -  private _cachedEdits: FileEdit[] | undefined; -  private _fetchNewEdit: ( -    debugContext: SerializedDebugContext -  ) => Promise<FileEdit[] | undefined>; -  private _debounceTimer: NodeJS.Timeout | undefined; - -  private _debugContextChanged(debugContext: SerializedDebugContext): boolean { -    if (!this._lastDebugContext) { -      return true; -    } - -    return ( -      JSON.stringify(this._lastDebugContext) !== JSON.stringify(debugContext) -    ); -  } - -  private _debugContextComplete(debugContext: SerializedDebugContext): boolean { -    return debugContext.rangesInFiles.length > 0; -  } - -  public async preloadEdit(debugContext: SerializedDebugContext) { -    if (this._debounceTimer) { -      clearTimeout(this._debounceTimer); -    } -    if ( -      this._debugContextComplete(debugContext) && -      this._debugContextChanged(debugContext) -    ) { -      this._debounceTimer = setTimeout(async () => { -        console.log("Preloading edits"); -        this._cachedEdits = await this._fetchNewEdit(debugContext); -        this._lastDebugContext = debugContext; -      }, 200); -    } -  } - -  public async getEdit( -    debugContext: SerializedDebugContext -  ): Promise<FileEdit[]> { -    if (this._debugContextChanged(debugContext)) { -      console.log("Cache miss"); -      this._cachedEdits = await this._fetchNewEdit(debugContext); -    } else { -      console.log("Cache hit"); -    } - -    return this._cachedEdits!; -  } - -  constructor( -    fetchNewEdit: ( -      debugContext: SerializedDebugContext -    ) => Promise<FileEdit[] | undefined> -  ) { -    this._fetchNewEdit = fetchNewEdit; -  } -} diff --git a/extension/react-app/src/util/index.ts b/extension/react-app/src/util/index.ts index 458f9d95..c4168e13 100644 --- a/extension/react-app/src/util/index.ts +++ b/extension/react-app/src/util/index.ts @@ -1,27 +1,43 @@ -import { RangeInFile } from "../../../src/client"; +type Platform = "mac" | "linux" | "windows" | "unknown"; -export function readRangeInVirtualFileSystem( -  rangeInFile: RangeInFile, -  filesystem: { [filepath: string]: string } -): string | undefined { -  const range = rangeInFile.range; - -  let data = filesystem[rangeInFile.filepath]; -  if (data === undefined) { -    console.log("File not found"); -    return undefined; +export function getPlatform(): Platform { +  const platform = window.navigator.platform.toUpperCase(); +  if (platform.indexOf("MAC") >= 0) { +    return "mac"; +  } else if (platform.indexOf("LINUX") >= 0) { +    return "linux"; +  } else if (platform.indexOf("WIN") >= 0) { +    return "windows";    } else { -    let lines = data.toString().split("\n"); -    if (range.start.line === range.end.line) { -      return lines[rangeInFile.range.start.line].slice( -        rangeInFile.range.start.character, -        rangeInFile.range.end.character -      ); -    } else { -      let firstLine = lines[range.start.line].slice(range.start.character); -      let lastLine = lines[range.end.line].slice(0, range.end.character); -      let middleLines = lines.slice(range.start.line + 1, range.end.line); -      return [firstLine, ...middleLines, lastLine].join("\n"); -    } +    return "unknown"; +  } +} + +export function isMetaEquivalentKeyPressed(event: { +  metaKey: boolean; +  ctrlKey: boolean; +}): boolean { +  const platform = getPlatform(); +  switch (platform) { +    case "mac": +      return event.metaKey; +    case "linux": +    case "windows": +      return event.ctrlKey; +    default: +      return event.metaKey; +  } +} + +export function getMetaKeyLabel(): string { +  const platform = getPlatform(); +  switch (platform) { +    case "mac": +      return "⌘"; +    case "linux": +    case "windows": +      return "^"; +    default: +      return "⌘";    }  } | 
