Skip to content
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

Implement AutocompleteMetadata and add autofills #28

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ All notable changes to the "roblox-lua-autofills" extension will be documented i

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [Unreleased]
- Added support for the Lua Libraries available in Roblox (math, string, os, etc.).
- Added support for static methods and properties of Roblox Datatypes (Vector3, Color3, etc.).
- Fixed Instance.new popping up whenever you typed a "." (dot).

## [1.7.1]
- Service auto-importer will now suggest for non-idiomatic whitespace in service declarations.

Expand Down
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@
"@types/mocha": "^2.2.42",
"@types/node": "^8.3.0",
"@types/request-promise-native": "^1.0.15",
"@types/xml2js": "^0.4.5",
"tslint": "^5.8.0",
"typescript": "^3.6.2",
"vscode": "^1.1.36"
},
"dependencies": {
"request": "^2.88.0",
"request-promise-native": "^1.0.7"
"request-promise-native": "^1.0.7",
"xml2js": "^0.4.23"
}
}
}
207 changes: 207 additions & 0 deletions src/autocompleteDump.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import * as request from "request-promise-native"
import * as vscode from "vscode"
import { parseStringPromise } from "xml2js"

const AUTOCOMPLETE_METADATA = "https://raw.githubusercontent.com/CloneTrooper1019/Roblox-Client-Tracker/roblox/AutocompleteMetadata.xml"

export interface AutocompleteParameter {
type: string,
name: string,
optional: boolean,
constraint?: string,
}

export interface AutocompleteReturn {
type: string,
name?: string,
}

export interface AutocompleteFunction {
name: string,
static: boolean,
description?: string,
parameters: AutocompleteParameter[],
returns: AutocompleteReturn[],
}

export interface AutocompleteProperty {
name: string,
static: boolean,
type: string,
description?: string,
}

export interface AutocompleteGroup {
name: string,
functions: AutocompleteFunction[],
properties: AutocompleteProperty[],
}

export interface AutocompleteDump {
LuaLibrary: AutocompleteGroup[],
ItemStruct: AutocompleteGroup[],
}

// Interfaces for the data provided in the XML parser
interface XMLPropertyPart {
_: string, // Description
$: {
name: string,
static?: string,
}
}

interface XMLOrderedPropertyPart extends XMLPropertyPart {
"#name": string // Property type
}

interface XMLProperty {
$$: XMLOrderedPropertyPart[],
[name: string]: XMLPropertyPart[]
}

interface XMLFunctionReturnPart {
$?: {
name?: string,
}
}
interface XMLOrderedFunctionReturnPart extends XMLFunctionReturnPart { "#name": string }

interface XMLFunctionParameterPart {
$: {
name: string,
constraint?: string,
optional?: string,
},
}
interface XMLOrderedFunctionParameterPart extends XMLFunctionParameterPart { "#name": string }

interface XMLFunction {
$: {
name: string,
static: string,
},
$$: [], // Shouldn't really be used
returns: Array<{
$$: XMLOrderedFunctionReturnPart[],
[name: string]: XMLFunctionReturnPart[],
}>,
parameters: Array<{
$$: XMLOrderedFunctionParameterPart[],
[name: string]: XMLFunctionParameterPart[],
}>,
description: string[],
}

interface XMLGroup {
$: {
name: string,
},
$$: [], // Shouldnt be used
Function?: XMLFunction[],
Properties?: XMLProperty[],
}

interface XMLTree {
StudioAutocomplete: {
LuaLibrary: XMLGroup[],
ItemStruct: XMLGroup[],
CoreLibrary: XMLGroup[]
ReservedWords: Array<{
$$: Array<{
$: {
name: string,
},
"#name": string,
}>,
Keyword: Array<{
$: {
name: string,
},
}>,
}>,
}
}

const formatFunction = (func: XMLFunction): AutocompleteFunction => {
const attributes = func.$
const parameters: AutocompleteParameter[] =
(func.parameters && func.parameters[0].$$) ? func.parameters[0].$$.map(paramObj => {
const type = paramObj["#name"]
const parameterAttributes = paramObj.$
return {
constraint: parameterAttributes.constraint,
name: parameterAttributes.name,
optional: parameterAttributes.optional === "true",
type,
}
}) : []

const returns = (func.returns && func.returns[0].$$) ? func.returns[0].$$.map(ret => {
const type = ret["#name"]
const returnAttributes = ret.$
return {
name: returnAttributes && returnAttributes.name,
type,
}
}) : []

return {
description: func.description && func.description[0],
name: attributes.name,
parameters,
returns,
static: attributes.static === "true",
}
}

const formatProperty = (property: XMLOrderedPropertyPart): AutocompleteProperty => {
const description = property._
const type = property["#name"]
const attributes = property.$
return {
description,
name: attributes.name,
static: attributes.static === "true",
type,
}
}

const formatGroup = (group: XMLGroup): AutocompleteGroup => {
return {
functions: group.Function ? group.Function.map(formatFunction) : [],
name: group.$.name,
properties: (group.Properties && group.Properties[0].$$) ? group.Properties[0].$$.map(formatProperty) : [],
}
}

const formatter = (tree: XMLTree): AutocompleteDump => {
const root = tree.StudioAutocomplete
const LuaLibrary = root.LuaLibrary
const ItemStruct = root.ItemStruct

const updatedLibrary = LuaLibrary.map(formatGroup)
const updatedItemStruct = ItemStruct.map(formatGroup)

return {
ItemStruct: updatedItemStruct,
LuaLibrary: updatedLibrary,
}
}

const autocompleteMetadataPromise = (async () => {
const data = await request(AUTOCOMPLETE_METADATA).catch((err) => {
vscode.window.showErrorMessage("Error downloading Autocomplete Metadata dump", err.toString())
})
const output = await parseStringPromise(data, {
explicitChildren: true,
preserveChildrenOrder: true,
trim: true,
})

return formatter(output)
})()

export function getAutocompleteDump(): Promise<AutocompleteDump> {
return autocompleteMetadataPromise
}
6 changes: 4 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import * as vscode from "vscode"
import { RobloxColorProvider } from "./color"
import { Companion } from "./companion"
import { EnumCompletionProvider } from "./enum"
import { InstanceCompletionProvider } from "./instance"
import { ItemStructCompletionProvider } from "./itemStruct"
import { LuaLibraryCompletionProvider } from "./luaLibrary"
import { RojoHandler } from "./rojo"
import { ServiceCompletionProvider } from "./services"
const SELECTOR = { scheme: "file", language: "lua" }
Expand Down Expand Up @@ -38,7 +39,8 @@ export async function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(vscode.languages.registerColorProvider(SELECTOR, new RobloxColorProvider()))

context.subscriptions.push(vscode.languages.registerCompletionItemProvider(SELECTOR, new InstanceCompletionProvider(), "."))
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(SELECTOR, new EnumCompletionProvider(), "."))
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(SELECTOR, new ServiceCompletionProvider(), ".", ":"))
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(SELECTOR, new LuaLibraryCompletionProvider(), "."))
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(SELECTOR, new ItemStructCompletionProvider(), "."))
}
37 changes: 0 additions & 37 deletions src/instance.ts

This file was deleted.

Loading