-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add support for React components #5
Open
BrunnerLivio
wants to merge
1
commit into
agillispie:main
Choose a base branch
from
BrunnerLivio:feature/components
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -8,12 +8,19 @@ export function activate(context: vscode.ExtensionContext) { | |
if (editor) { | ||
const position = editor.selection.active; | ||
const line = editor.document.lineAt(position.line).text; | ||
const isPatternPresent = /use(state|effect)\.\w+.*$/i.test(line); | ||
const isReactHookPatternPresent = /use(state|effect)\.\w+.*$/i.test(line); | ||
|
||
vscode.commands.executeCommand( | ||
"setContext", | ||
"isReactHookPattern", | ||
isPatternPresent | ||
isReactHookPatternPresent | ||
); | ||
|
||
const isComponentPatternPresent = /(fc|cc)\.\w+.*$/i.test(line); | ||
vscode.commands.executeCommand( | ||
"setContext", | ||
"isReactComponentPattern", | ||
isComponentPatternPresent | ||
); | ||
} | ||
}); | ||
|
@@ -61,7 +68,50 @@ export function activate(context: vscode.ExtensionContext) { | |
} | ||
); | ||
|
||
context.subscriptions.push(disposable); | ||
let disposable2 = vscode.commands.registerCommand( | ||
"extension.generateReactComponentSnippet", | ||
() => { | ||
const editor = vscode.window.activeTextEditor; | ||
if (!editor) { | ||
return; | ||
} | ||
|
||
const position = editor.selection.active; | ||
const line = editor.document.lineAt(position.line).text; | ||
|
||
const componentRegex = /(fc|cc)\.(\w+)(\[(.*)\])?$/i; | ||
const match = line.match(componentRegex); | ||
|
||
if (!match) { | ||
return; | ||
} | ||
|
||
const componentType = match[1].toLowerCase(); | ||
const componentName = match[2]; | ||
const componentProps = match[4]; | ||
|
||
let snippet = ""; | ||
|
||
if (componentType === "fc") { | ||
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. This could be rewritten to use a switch/case instead or even better to use an appropriate data structure so you eliminate any branching code in the first place. But I leave that refactoring to another PR / to you :) |
||
snippet = generateFunctionalComponentSnippet( | ||
componentName, | ||
componentProps | ||
); | ||
} else if (componentType === "cc") { | ||
snippet = generateClassComponentSnippet(componentName); | ||
} | ||
|
||
// Replace the line with the generated snippet | ||
editor.edit((editBuilder) => { | ||
const range = new vscode.Range( | ||
new vscode.Position(position.line, 0), | ||
new vscode.Position(position.line, line.length) | ||
); | ||
editBuilder.replace(range, snippet); | ||
}); | ||
} | ||
); | ||
context.subscriptions.push(disposable2); | ||
|
||
// Helper functions | ||
function capitalizeFirstLetter(string: string): string { | ||
|
@@ -83,4 +133,16 @@ export function activate(context: vscode.ExtensionContext) { | |
const functionCall = isAsync ? `${functionName}();` : `${functionName}();`; | ||
return `useEffect(() => {\n ${asyncSnippet}function ${functionName}() {\n // Your code here\n }\n ${functionCall}\n}, ${dependencies});`; | ||
} | ||
|
||
function generateFunctionalComponentSnippet( | ||
componentName: string, | ||
componentProps?: string | ||
): string { | ||
const props = componentProps ? `{${componentProps}}` : ""; | ||
return `const ${componentName} = (${props}) => {\n return (\n <div></div>\n );\n};`; | ||
} | ||
|
||
function generateClassComponentSnippet(componentName: string): string { | ||
return `class ${componentName} extends React.Component {\n render() {\n return (\n <div></div>\n );\n }\n}`; | ||
} | ||
} |
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.
I'd encourage using early returns instead of nesting your code, like you did in the
extension.generateReactHookSnippet
command.This leads to much simpler code in my eyes and also decreases the cyclomatic complexity
CodeAesthetics made a video about it if you're curious: https://www.youtube.com/watch?v=CFRhGnuXG-4