-
Notifications
You must be signed in to change notification settings - Fork 7
Add autofills when using Roact.createElement #42
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
Open
JohnnyMorganz
wants to merge
7
commits into
Kampfkarren:master
Choose a base branch
from
JohnnyMorganz:roact
base: master
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4881436
Implement Roact.createElement autofill support
JohnnyMorganz b51d8b1
Update CHANGELOG.md
JohnnyMorganz 5432409
Filter out for creatable instances only
JohnnyMorganz 00d364e
Add autofills for element name
JohnnyMorganz c6bc286
Update CHANGELOG.md
JohnnyMorganz e863f99
Prevent items showing up when entering a value
JohnnyMorganz f81eb3d
Cleanup
JohnnyMorganz 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
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,149 @@ | ||
| import * as vscode from "vscode" | ||
| import { ApiClass, ApiPropertySecurity, getApiDump, UNCREATABLE_TAGS } from "./dump" | ||
|
|
||
| const UNSCRIPTABLE_TAGS: Set<string> = new Set([ | ||
| "Deprecated", | ||
| "Hidden", | ||
| "NotBrowsable", | ||
| "NotScriptable", | ||
| ]) | ||
|
|
||
| const isCreatableInstance = (klass: ApiClass) => klass.Tags === undefined || | ||
| klass.Tags.every(tag => !UNCREATABLE_TAGS.has(tag)) | ||
|
|
||
| export class RoactCompletionProvider implements vscode.CompletionItemProvider { | ||
| instances: Promise<Map<string, ApiClass>> | ||
| creatableInstancesItems: Promise<vscode.CompletionItem[]> | ||
|
|
||
| constructor() { | ||
| this.instances = getApiDump().then(dump => { | ||
| const output = new Map() | ||
|
|
||
| for (const klass of dump.Classes) { | ||
| const klassData = { | ||
| Description: klass.Description, | ||
| Members: klass.Members.filter(member => member.Tags === undefined || | ||
| member.Tags.every(tag => !UNSCRIPTABLE_TAGS.has(tag))), | ||
| MemoryCategory: klass.MemoryCategory, | ||
| Name: klass.Name, | ||
| Superclass: klass.Superclass, | ||
| Tags: klass.Tags, | ||
| } | ||
|
|
||
| output.set(klass.Name, klassData) | ||
| } | ||
|
|
||
| return output | ||
| }) | ||
|
|
||
| this.creatableInstancesItems = getApiDump().then(dump => { | ||
| const completionItems: vscode.CompletionItem[] = [] | ||
|
|
||
| for (const klass of dump.Classes.filter(isCreatableInstance)) { | ||
| const completionItem = new vscode.CompletionItem( | ||
| klass.Name, | ||
| vscode.CompletionItemKind.Constant, | ||
| ) | ||
|
|
||
| completionItem.detail = `(class) ${klass.Name}` | ||
| completionItem.documentation = new vscode.MarkdownString(`[Developer Reference](https://developer.roblox.com/en-us/api-reference/class/${klass.Name})`) | ||
| completionItems.push(completionItem) | ||
| } | ||
|
|
||
| return completionItems | ||
| }) | ||
| } | ||
|
|
||
| public async createCompletionItems( | ||
| service: ApiClass, | ||
| ): Promise<vscode.CompletionItem[]> { | ||
| let completionItems: vscode.CompletionItem[] = [] | ||
|
|
||
| for (const member of service.Members) { | ||
| if (member.MemberType === "Property") { | ||
| const security = member.Security as ApiPropertySecurity | ||
| if (security.Read !== "None" && security.Write !== "None") { | ||
| continue | ||
| } | ||
|
|
||
| const completionItem = new vscode.CompletionItem( | ||
| member.Name, | ||
| vscode.CompletionItemKind.Field, | ||
| ) | ||
| completionItem.insertText = `${member.Name} = ` | ||
| completionItem.detail = `(property) ${service.Name}.${member.Name}: ${member.ValueType ? member.ValueType.Name : "unknown"}` | ||
| completionItem.documentation = new vscode.MarkdownString(`[Developer Reference](https://developer.roblox.com/en-us/api-reference/event/${service.Name}/${member.Name})`) | ||
| completionItems.push(completionItem) | ||
| } else if (member.MemberType === "Event") { | ||
| if (member.Security !== "None") { | ||
| continue | ||
| } | ||
|
|
||
| const completionItem = new vscode.CompletionItem( | ||
| member.Name, | ||
| vscode.CompletionItemKind.Event, | ||
| ) | ||
| completionItem.insertText = `[Roact.Event.${member.Name}] = ` | ||
| completionItem.detail = `(event) ${service.Name}.${member.Name}(${member.Parameters.map(parameter => `${parameter.Name}: ${parameter.Type ? parameter.Type.Name : "unknown"}`).join(", ")})` | ||
| completionItem.documentation = new vscode.MarkdownString(`[Developer Reference](https://developer.roblox.com/en-us/api-reference/property/${service.Name}/${member.Name})`) | ||
| completionItems.push(completionItem) | ||
| } | ||
| } | ||
|
|
||
| if (service.Superclass) { | ||
| const klass = (await this.instances).get(service.Superclass) | ||
| if (klass) { | ||
| const inheritedMembers = await this.createCompletionItems(klass) | ||
| for (const completionItem of inheritedMembers) { | ||
| if (completionItem.documentation !== undefined) { | ||
| (completionItem.documentation as vscode.MarkdownString).value = `Inherited from ${service.Superclass}\n\n${(completionItem.documentation as vscode.MarkdownString).value}` | ||
| } | ||
| } | ||
| completionItems = completionItems.concat(inheritedMembers) | ||
| } | ||
| } | ||
|
|
||
| return completionItems | ||
| } | ||
|
|
||
| public async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position) { | ||
| const text = document.getText(new vscode.Range(new vscode.Position(0, 0), position)) | ||
| const functionMatch = text.match(/([\w.]+)\(["'](\w+)["'],\s*{[\w\s\d.-="',:/()\[\]]*$/) | ||
| if (functionMatch !== null) { | ||
| const callable = functionMatch[1] | ||
|
|
||
| // Check to see if there is an alias | ||
| const aliasMatch = text.match(/^local\s+(\w+)\s*=\s*Roact\.createElement\s*$/m) | ||
|
|
||
| if (callable === "Roact.createElement" || (aliasMatch !== null && callable === aliasMatch[1])) { | ||
| const lineText = document.lineAt(position.line).text | ||
| // Don't provide items when entering a value | ||
| // This is done by counting the number of = and the number of matching , | ||
| // (Comma is checked as multiple items can be assigned on one line) | ||
| const equalsCount = lineText.match(/=/g)?.length | ||
| const commaCount = lineText.match(/,/g)?.length | ||
|
|
||
| if (equalsCount === undefined || (commaCount !== undefined && commaCount >= equalsCount)) { | ||
| const availableInstances = (await this.instances) | ||
| const instance = availableInstances.get(functionMatch[2]) | ||
| if (instance !== undefined && isCreatableInstance(instance)) { | ||
| return this.createCompletionItems(instance) | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| const beginningCallMatch = text.match(/([\w.]+)\(["']\w*$/) | ||
| if (beginningCallMatch !== null) { | ||
| // Provide autocomplete to the first argument to Roact.createElement | ||
| const callable = beginningCallMatch[1] | ||
|
|
||
| // Check to see if there is an alias | ||
| const aliasMatch = text.match(/^local\s+(\w+)\s*=\s*Roact\.createElement\s*$/m) | ||
|
|
||
| if (callable === "Roact.createElement" || (aliasMatch !== null && callable === aliasMatch[1])) { | ||
| return this.creatableInstancesItems | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.