-
Notifications
You must be signed in to change notification settings - Fork 94
added format shortcut key in editor #2385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Sahil-Gupta584
wants to merge
9
commits into
tscircuit:main
from
Sahil-Gupta584:editor-format-shortcut
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1498754
added format shortcut key in editor
Sahil-Gupta584 e3a60f3
format
Sahil-Gupta584 972f327
Merge branch 'main' into editor-format-shortcut
ArnavK-09 8a44ff3
add-format-shortcut-key
Sahil-Gupta584 d48d589
Merge branch 'editor-format-shortcut' of https://github.com/Sahil-Gup…
Sahil-Gupta584 3c4bf4d
format
Sahil-Gupta584 9ce24c9
ts fix
Sahil-Gupta584 04eed19
try block enhancement
Sahil-Gupta584 f4320d5
Merge branch 'main' into editor-format-shortcut
rushabhcodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -27,7 +27,7 @@ import { loadDefaultLibMap, fetchWithPackageCaching } from "@/lib/ts-lib-cache" | |||||
| import { tsAutocomplete, tsFacet, tsSync } from "@valtown/codemirror-ts" | ||||||
| import { getLints } from "@valtown/codemirror-ts" | ||||||
| import { EditorView } from "codemirror" | ||||||
| import { useEffect, useMemo, useRef, useState } from "react" | ||||||
| import { useCallback, useEffect, useMemo, useRef, useState } from "react" | ||||||
| import tsModule from "typescript" | ||||||
| import CodeEditorHeader, { | ||||||
| FileName, | ||||||
|
|
@@ -52,6 +52,8 @@ import { inlineCopilot } from "codemirror-copilot" | |||||
| import { useViewTsFilesDialog } from "@/components/dialogs/view-ts-files-dialog" | ||||||
| import { Loader2 } from "lucide-react" | ||||||
| import { useAxios } from "@/hooks/use-axios" | ||||||
| import { useToast } from "@/hooks/use-toast" | ||||||
| import { handleFormatFile as handleFormatFileRaw } from "@/lib/utils/handle-format-file" | ||||||
|
|
||||||
| const defaultImports = ` | ||||||
| import React from "@types/react/jsx-runtime" | ||||||
|
|
@@ -139,6 +141,11 @@ export const CodeEditor = ({ | |||||
| }) | ||||||
| const { Dialog: ViewTsFilesDialog, openDialog: openViewTsFilesDialog } = | ||||||
| useViewTsFilesDialog() | ||||||
| const { toast } = useToast() | ||||||
|
|
||||||
| const handleFormatFile = useCallback(() => { | ||||||
| handleFormatFileRaw({ files, currentFile, updateFileContent, toast }) | ||||||
| }, [currentFile, files, toast]) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| const entryPointFileName = useMemo(() => { | ||||||
| const entryPointFile = findTargetFile({ files, filePathFromUrl: null }) | ||||||
|
|
@@ -308,6 +315,13 @@ export const CodeEditor = ({ | |||||
| return true | ||||||
| }, | ||||||
| }, | ||||||
| { | ||||||
| key: "Shift-Alt-f", | ||||||
| run: () => { | ||||||
| handleFormatFile() | ||||||
| return true | ||||||
| }, | ||||||
| }, | ||||||
| ]), | ||||||
| ), | ||||||
| keymap.of([indentWithTab]), | ||||||
|
|
@@ -838,6 +852,7 @@ export const CodeEditor = ({ | |||||
| aiAutocompleteEnabled, | ||||||
| setAiAutocompleteEnabled, | ||||||
| ]} | ||||||
| handleFormatFile={handleFormatFile} | ||||||
| /> | ||||||
| )} | ||||||
| <div | ||||||
|
|
||||||
This file contains hidden or 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 hidden or 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,99 @@ | ||
| import { ToasterToast } from "@/hooks/use-toast" | ||
| import { PackageFile } from "@/types/package" | ||
|
|
||
| export function handleFormatFile({ | ||
| files, | ||
| currentFile, | ||
| updateFileContent, | ||
| toast, | ||
| }: { | ||
| files: PackageFile[] | ||
| currentFile: string | null | ||
| updateFileContent: (path: string, content: string) => void | ||
| toast: (toastData: ToasterToast) => string | ||
| }) { | ||
| if (!window.prettier || !window.prettierPlugins) return | ||
| if (!currentFile) return | ||
|
|
||
| const currentFileObj = files.find((f) => f.path === currentFile) | ||
| const currentContent = currentFileObj?.content | ||
|
|
||
| if (!currentContent || currentContent.trim().length === 0) { | ||
| toast({ | ||
| title: "Empty file", | ||
| description: "Cannot format an empty file.", | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| let fileExtension = currentFile.split(".").pop()?.toLowerCase() | ||
|
|
||
| if (!fileExtension || fileExtension === currentFile.toLowerCase()) { | ||
| if (["readme"].includes(currentFile.toLowerCase())) { | ||
| fileExtension = "md" | ||
| } else { | ||
| toast({ | ||
| title: "Cannot determine file type", | ||
| description: "Unable to format file without an extension.", | ||
| }) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // Handle JSON formatting separately | ||
| if (fileExtension === "json") { | ||
| try { | ||
| const jsonObj = JSON.parse(currentContent) | ||
| const formattedJson = JSON.stringify(jsonObj, null, 2) | ||
| updateFileContent(currentFile, formattedJson) | ||
| } catch (jsonError) { | ||
| toast({ | ||
| title: "Invalid JSON", | ||
| description: "Failed to format JSON: invalid syntax.", | ||
| variant: "destructive", | ||
| }) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| const parserMap: Record<string, string> = { | ||
| js: "babel", | ||
| jsx: "babel", | ||
| ts: "typescript", | ||
| tsx: "typescript", | ||
| md: "markdown", | ||
| markdown: "markdown", | ||
| } | ||
|
|
||
| const parser = parserMap[fileExtension] || "tsx" | ||
|
|
||
| try { | ||
| const formattedCode = window.prettier.format(currentContent, { | ||
| semi: false, | ||
| parser: parser, | ||
| plugins: window.prettierPlugins, | ||
| }) | ||
|
|
||
| updateFileContent(currentFile, formattedCode) | ||
| } catch (error) { | ||
| console.error("Formatting error:", error) | ||
| if ( | ||
| error instanceof Error && | ||
| error.message.includes("No parser could be inferred") | ||
| ) { | ||
| toast({ | ||
| title: "Unsupported File Type", | ||
| description: `Formatting not supported for .${fileExtension} files. Tried default parser.`, | ||
| }) | ||
| } else { | ||
| toast({ | ||
| title: "Formatting error", | ||
| description: | ||
| error instanceof Error | ||
| ? error.message | ||
| : "Failed to format the code. Please check for syntax errors.", | ||
| variant: "destructive", | ||
| }) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toast dep?