-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(vscode:extension): split useFileState into useIndex hook and…
… getFileState func that can be used with or without useMemo
- Loading branch information
Showing
5 changed files
with
167 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { | ||
ConstEntity, | ||
InterfaceEntity, | ||
ComponentEntity, | ||
Component, | ||
TypeEntity, | ||
File, | ||
Const, | ||
Interface, | ||
} from "../generated/sourcecode"; | ||
import * as ts from "../generated/typesystem"; | ||
import { VSCodeMessageData } from "./use_index"; | ||
|
||
// File state is grouped and sorted render-friendly object | ||
interface FileState { | ||
imports: Array<{ alias: string; path: string }>; | ||
entities: GroupedEntities; | ||
} | ||
|
||
// we use arrays instead of objects because it's faster to render | ||
interface GroupedEntities { | ||
types: Array<{ name: string; entity: ts.Def }>; | ||
interfaces: Array<{ name: string; entity: Interface }>; | ||
constants: Array<{ name: string; entity: Const }>; | ||
components: Array<{ name: string; entity: Component }>; | ||
} | ||
|
||
export function getFileState(state: VSCodeMessageData | undefined): FileState { | ||
const result: FileState = { | ||
imports: [], | ||
entities: { types: [], interfaces: [], constants: [], components: [] }, | ||
}; | ||
|
||
// if tab opened first time and there were no updates from vscode yet | ||
if (!state) { | ||
return result; | ||
} | ||
|
||
if (!state.programState.packages) { | ||
return result; | ||
} | ||
|
||
const { currentFileName, currentPackageName } = getCurrentPackageAndFileName( | ||
state.openedDocument.fileName, | ||
state.workspaceUri.path | ||
); | ||
|
||
const currentFile: File = | ||
state.programState.packages![currentPackageName][currentFileName]; | ||
|
||
for (const alias in currentFile.imports) { | ||
result.imports.push({ | ||
alias, | ||
path: currentFile.imports[alias], | ||
}); | ||
} | ||
|
||
result.imports.sort(); | ||
|
||
// object to array for faster rendering | ||
for (const name in currentFile.entities) { | ||
const entity = currentFile.entities[name]; | ||
switch (entity.kind) { | ||
case TypeEntity: | ||
if (entity.type === undefined) { | ||
continue; | ||
} | ||
result.entities.types.push({ | ||
name: name, | ||
entity: entity.type as ts.Def, | ||
}); | ||
break; | ||
case ConstEntity: | ||
if (entity.const === undefined) { | ||
break; | ||
} | ||
result.entities.constants.push({ | ||
name: name, | ||
entity: entity.const, | ||
}); | ||
break; | ||
case InterfaceEntity: | ||
if (entity.interface === undefined) { | ||
break; | ||
} | ||
result.entities.interfaces.push({ | ||
name: name, | ||
entity: entity.interface, | ||
}); | ||
break; | ||
case ComponentEntity: | ||
if (entity.component === undefined) { | ||
break; | ||
} | ||
result.entities.components.push({ | ||
name: name, | ||
entity: entity.component, | ||
}); | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
const getCurrentPackageAndFileName = ( | ||
openedFileName: string, | ||
workspacePath: string | ||
) => { | ||
const relativePath = openedFileName.replace(workspacePath + "/", ""); | ||
const pathParts = relativePath.split("/"); | ||
|
||
const currentPackageName = pathParts.slice(0, -1).join("/"); // all but the last segment (filename) | ||
const currentFileNameWithExtension = pathParts[pathParts.length - 1]; // last semgent (filename) | ||
const currentFileName = currentFileNameWithExtension.split(".")[0]; // filename without .neva | ||
|
||
return { currentPackageName, currentFileName }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { TextDocument, Uri } from "vscode"; | ||
import { useState, useEffect } from "react"; | ||
import { Module } from "../generated/sourcecode"; | ||
|
||
export interface VSCodeMessageData { | ||
workspaceUri: Uri; | ||
openedDocument: TextDocument; | ||
programState: Module; | ||
isDarkTheme: boolean; | ||
} | ||
|
||
const vscodeApi = acquireVsCodeApi<VSCodeMessageData>(); | ||
|
||
export function useIndex(): VSCodeMessageData | undefined { | ||
const persistedState = vscodeApi.getState(); | ||
const [state, setState] = useState<VSCodeMessageData | undefined>( | ||
persistedState | ||
); | ||
|
||
useEffect(() => { | ||
const listener = (event: { data: VSCodeMessageData }) => { | ||
setState(event.data); | ||
vscodeApi.setState(event.data); | ||
}; | ||
window.addEventListener("message", listener); | ||
return () => window.removeEventListener("message", listener); | ||
}, []); | ||
|
||
return state; | ||
} |
This file was deleted.
Oops, something went wrong.