Skip to content
This repository has been archived by the owner on Jul 26, 2020. It is now read-only.

Commit

Permalink
Completed new Manual Scrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbox-irl committed Aug 23, 2018
1 parent 5a7bb0f commit 37fb2e4
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 104 deletions.
119 changes: 77 additions & 42 deletions src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,45 +81,80 @@ export type enumsMacros = [string[], MacroPackage[]];

export type OtherResources = [string[], CompletionItemKind];

export interface DocFunction {
name: string;
signature: string;
parameters: DocParams[];
minParameters: number;
maxParameters: number;
example: DocExample;
documentation: string;
return: string;
link: string;
isBritish?: boolean;
}

export interface DocParams {
label: string;
documentation: string;
}

export interface DocExample {
code: string;
description: string;
}

export interface GMLDocs {
functions: DocFunction[];
variables: DocVariable[];
}

export interface DocVariable {
name: string;
example: DocExample;
documentation: string;
type: string;
link: string;
object: string;
isBritish?: boolean;
}

export const enum DocType {
function = 0,
variable = 1
}
/**
* Namespace describing all GMLDocs interfaces.
*/
export namespace GMLDocs {
/**
* This describes the saved DocFile we create.
*/
export interface DocFile {
functions: DocFunction[];
variables: DocVariable[];
}

/**
* Scrapped Documentation Function information.
*/
export interface DocFunction {
name: string;
signature: string;
parameters: DocParams[];
minParameters: number;
maxParameters: number;
example: DocExample;
documentation: string;
return: string;
link: string;
doNotAutoComplete?: boolean;
}
/**
* Scrapped Documentation Variable information.
*/
export interface DocVariable {
name: string;
example: DocExample;
documentation: string;
type: string;
link: string;
object: string;
doNotAutoComplete?: boolean;
}

/**
* Scrapped Documentation Parameter information.
*/
export interface DocParams {
label: string;
documentation: string;
}
/**
* Scrapped Documentation Example information.
*/
export interface DocExample {
code: string;
description: string;
}

/**
* Enum used very briefly in the `documentationImporter` to
* differentiate functions and variables by the presence of
* "()" in their "signature" property.
*/
export const enum DocType {
function = 0,
variable = 1
}
}

export namespace GMLToolsSettings {
export interface Config {
preferredSpellings: SpellingSettings;
}

export const enum SpellingSettings {
american = "American",
british = "British",
noPref = "No Preference"
}
}
40 changes: 25 additions & 15 deletions src/documentationImporter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DocFunction, DocParams, GMLDocs, DocType, DocVariable } from "./declarations";
import { GMLDocs, GMLToolsSettings } from "./declarations";
import * as path from "path";
import * as fse from "fs-extra";
import * as AdmZip from "adm-zip";
Expand Down Expand Up @@ -46,7 +46,7 @@ export class DocumentationImporter {
this.UKSpellingsA = Object.getOwnPropertyNames(this.UKSpellings);
}

public async createManual(): Promise<GMLDocs> {
public async createManual(): Promise<GMLDocs.DocFile> {
// Get our path to the GMS2 Program folder. If nothing there,
// exit early.
const gms2FPath = await this.getManualPath();
Expand All @@ -63,7 +63,7 @@ export class DocumentationImporter {
const secondaryDocs = "source/_build/3_scripting/3_gml_overview";

// Main docs
let gmlDocs: GMLDocs = {
let gmlDocs: GMLDocs.DocFile = {
functions: [],
variables: []
};
Expand Down Expand Up @@ -102,7 +102,7 @@ export class DocumentationImporter {
const $ = cheerio.load(thisZipEntry.getData().toString(), {
normalizeWhitespace: true
});
const thisFunction: DocFunction = {
const thisFunction: GMLDocs.DocFunction = {
documentation: "",
example: {
code: "",
Expand All @@ -117,7 +117,7 @@ export class DocumentationImporter {
link: "docs2.yoyogames.com/" + thisZipEntry.entryName
};

let resourceType: DocType;
let resourceType: GMLDocs.DocType;

try {
const docType = $("h3");
Expand Down Expand Up @@ -157,7 +157,7 @@ export class DocumentationImporter {
const isFunction = thisFunction.signature.includes("(");

if (isFunction) {
resourceType = DocType.function;
resourceType = GMLDocs.DocType.function;
thisFunction.name = thisFunction.signature.slice(
0,
thisFunction.signature.indexOf("(")
Expand Down Expand Up @@ -197,7 +197,7 @@ export class DocumentationImporter {
} else {
// cutt off the semicolon
thisFunction.name = thisFunction.signature.slice(0, -1);
resourceType = DocType.variable;
resourceType = GMLDocs.DocType.variable;
}
}

Expand Down Expand Up @@ -316,7 +316,7 @@ export class DocumentationImporter {
if (thisRow.name == "tr") {
// We could be indexing a parameter here, so let's make a guy!
let checkParam = false;
let thisParameter: DocParams = {
let thisParameter: GMLDocs.DocParams = {
documentation: "",
label: ""
};
Expand Down Expand Up @@ -387,7 +387,7 @@ export class DocumentationImporter {
failureList["May have been parsed incorrectly:"].push(thisFunction.name);
}
// Final Validation For Functions:
if (resourceType == DocType.function) {
if (resourceType == GMLDocs.DocType.function) {
const isValid = await this.functionValidator(thisFunction);

if (isValid) {
Expand All @@ -403,7 +403,7 @@ export class DocumentationImporter {
thisParam.documentation = this.clearLineTerminators(thisParam.documentation);
}

// Stupid, stupid British spelling Check:
// Stupid, stupid British spelling Check for the bipsy bopsy little crumpet men:
for (const thisSpelling of this.UKSpellingsA) {
if (thisFunction.name.includes(thisSpelling)) {
const americanVersion = Object.assign({}, thisFunction);
Expand All @@ -426,13 +426,23 @@ export class DocumentationImporter {
this.UKSpellings[thisSpelling]
);

// Figure out the Right spelling
if (
this.lsp.userSettings.preferredSpellings ==
GMLToolsSettings.SpellingSettings.british
) {
americanVersion.doNotAutoComplete = true;
} else if (
this.lsp.userSettings.preferredSpellings ==
GMLToolsSettings.SpellingSettings.american
) {
thisFunction.doNotAutoComplete = true;
}
gmlDocs.functions.push(americanVersion);
thisFunction.isBritish = true;
}
}

// Commit our Main Function here

gmlDocs.functions.push(thisFunction);
} else {
// Push our *invalid* functions here.
Expand All @@ -442,9 +452,9 @@ export class DocumentationImporter {
}
}

if (resourceType == DocType.variable) {
if (resourceType == GMLDocs.DocType.variable) {
// Create our Variable
const thisVar: DocVariable = {
const thisVar: GMLDocs.DocVariable = {
documentation: this.clearLineTerminators(thisFunction.documentation),
example: {
code: thisFunction.example.code,
Expand Down Expand Up @@ -477,7 +487,7 @@ export class DocumentationImporter {
);

gmlDocs.variables.push(americanVersion);
thisVar.isBritish = true;
thisVar.doNotAutoComplete = true;
}
}

Expand Down
22 changes: 14 additions & 8 deletions src/fileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ import * as upath from "upath";
import * as uuidv4 from "uuid/v4";
import URI from "vscode-uri/lib/umd";
import * as chokidar from "chokidar";
import {
SemanticsOption,
CreateObjPackage,
AddEventsPackage,
ResourceType,
DocFunction,
DocParams
} from "./declarations";
import { SemanticsOption, CreateObjPackage, AddEventsPackage, ResourceType } from "./declarations";
import * as rubber from "gamemaker-rubber";
import { Resource, EventType, EventNumber, YYP, YYPResource } from "yyp-typings";

Expand Down Expand Up @@ -665,9 +658,22 @@ export class FileSystem {
return await fse.readFile(path.join(this.projectDirectory, ".gml-tools", fileName), encoding);
}

/**
* Set cached file with a string. Will write over any other data.
* @param fileName Filename to create.
* @param textToSave Text to save, encoded as 'utf8'
*/
public async setCachedFileText(fileName: string, textToSave: string) {
await fse.writeFile(path.join(this.projectDirectory, ".gml-tools", fileName), textToSave, "utf8");
}

public async deletedCachedFile(fileName: string) {
await fse.unlink(path.join(this.projectDirectory, ".gml-tools", fileName));
}

public async deleteCache() {
await fse.rmdir(path.join(this.projectDirectory, ".gml-tools"));
}
//#endregion

//#region Special Indexing Methods
Expand Down
58 changes: 55 additions & 3 deletions src/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
CreateObjPackage,
LanguageService,
ResourceType,
GMLDocs
GMLDocs,
GMLToolsSettings
} from "./declarations";
import { DocumentationImporter } from "./documentationImporter";

Expand All @@ -38,6 +39,7 @@ export class LSP {
public reference: Reference;
public documentationImporter: DocumentationImporter;
public timer: timeUtil;
public userSettings: GMLToolsSettings.Config;

constructor(public connection: IConnection) {
this.connection = connection;
Expand All @@ -64,8 +66,12 @@ export class LSP {
// Let the FileSystem do its index thing...
await this.fsManager.initialWorkspaceFolders(workspaceFolder);

this.userSettings = await this.connection.workspace.getConfiguration({
section: "gml-tools"
});

// Check or Create the Manual:
let ourManual: GMLDocs;
let ourManual: GMLDocs.DocFile;
let cacheManual = false;
if ((await this.fsManager.isFileCached("gmlDocs.json")) == false) {
ourManual = await this.documentationImporter.createManual();
Expand All @@ -80,7 +86,53 @@ export class LSP {

// Cache the Manual:
if (cacheManual) {
// this.fsManager.setCachedFileText("gmlDocs.json", JSON.stringify(ourManual));
this.fsManager.setCachedFileText("gmlDocs.json", JSON.stringify(ourManual, null, 4));
}
}
}

public async findNewSettings(): Promise<{ [prop: string]: string }> {
if (!this.userSettings) return {};
// Get our Settings:
const newSettings = await this.connection.workspace.getConfiguration({
section: "gml-tools"
});

// Iterate over to find our changed settings:
const ourSettings = Object.keys(newSettings);
let changedSettings = {};

for (const thisSetting of ourSettings) {
if (JSON.stringify(newSettings[thisSetting]) != JSON.stringify(this.userSettings[thisSetting])) {
changedSettings[thisSetting] = newSettings[thisSetting];
}
}

// Commit our changed Configs
return changedSettings;
}

public async updateSettings(changedSettings: { [key: string]: string }) {
const newSettings = Object.keys(changedSettings);

// Iterate on the settings
for (const thisSetting of newSettings) {
if (thisSetting == "preferredSpellings") {
if (
changedSettings[thisSetting] == GMLToolsSettings.SpellingSettings.american ||
changedSettings[thisSetting] == GMLToolsSettings.SpellingSettings.british ||
changedSettings[thisSetting] == GMLToolsSettings.SpellingSettings.noPref
) {
this.userSettings.preferredSpellings = newSettings[thisSetting];

this.connection.window.showWarningMessage("Please Restart VSCode for Setting to Take Effect.");

try {
this.fsManager.deletedCachedFile("gmlDocs.json");
} catch (err) {
throw err;
}
}
}
}
}
Expand Down
Loading

0 comments on commit 37fb2e4

Please sign in to comment.