diff options
Diffstat (limited to 'src/CodeParser')
| -rw-r--r-- | src/CodeParser/CodeParser.ts | 10 | ||||
| -rw-r--r-- | src/CodeParser/CodeParserController.ts | 101 | ||||
| -rw-r--r-- | src/CodeParser/CppParser.ts | 131 |
3 files changed, 242 insertions, 0 deletions
diff --git a/src/CodeParser/CodeParser.ts b/src/CodeParser/CodeParser.ts new file mode 100644 index 0000000..5d0d0bf --- /dev/null +++ b/src/CodeParser/CodeParser.ts @@ -0,0 +1,10 @@ +import { Position, TextDocumentContentChangeEvent, TextEditor } from "vscode"; + +export default interface ICodeParser { + + /** + * @param {TextEditor} activeEditor The open active Editor where the event came from + * @param {TextDocumentContentChangeEvent} event Something in the document changed + */ + Parse(activeEditor: TextEditor, event: TextDocumentContentChangeEvent); +} diff --git a/src/CodeParser/CodeParserController.ts b/src/CodeParser/CodeParserController.ts new file mode 100644 index 0000000..6583cde --- /dev/null +++ b/src/CodeParser/CodeParserController.ts @@ -0,0 +1,101 @@ +import { Disposable, Position, TextDocumentContentChangeEvent, TextEditor, TextLine, window, workspace } from "vscode"; +import CodeParser from "./CodeParser"; +import CppParser from "./CppParser"; + +/** + * + * Checks if the event matches the specified guidelines and if a parser exists for this language + * + * @export + * @class CodeParserController + */ +export default class CodeParserController { + private disposable: Disposable; + private indicators: string[] = []; + + /** + * Creates an instance of CodeParserController + * + * @memberOf CodeParserController + */ + public constructor() { + const subscriptions: Disposable[] = []; + + this.readConfig(); + + // Hand off the event to the parser if a valid parser is found + workspace.onDidChangeTextDocument((event) => { + const activeEditor: TextEditor = window.activeTextEditor; + if (activeEditor && event.document === activeEditor.document) { + this.onEvent(activeEditor, event.contentChanges[0]); + } + }, this, subscriptions); + + this.disposable = Disposable.from(...subscriptions); + } + + /** + * + * Disposes of the subscriptions + * + * @memberOf CodeParserController + */ + public dispose() { + this.disposable.dispose(); + } + + /*************************************************************************** + Implementation + ***************************************************************************/ + + private readConfig() { + this.indicators.push("/**"); // TODO: make this customizable + } + + private check(activeEditor: TextEditor, event: TextDocumentContentChangeEvent): boolean { + if (activeEditor == null || event.text == null) { + return false; + } + const activeSelection: Position = activeEditor.selection.active; + const activeLine: TextLine = activeEditor.document.lineAt(activeSelection.line); + const activeChar: string = activeLine.text.charAt(activeSelection.character); + const startsWith: boolean = event.text.startsWith("\n") || event.text.startsWith("\r\n"); + + // Check if enter was pressed. Note the ! + if (!((activeChar === "") && startsWith)) { + return false; + } + + const cont: string = activeLine.text.trim(); + let found: boolean = false; + + this.indicators.forEach((element: string) => { + if (element === cont) { // Compare the content from the line with the valid indicators + found = true; + return; + } + }); + + return found; + } + + private onEvent(activeEditor: TextEditor, event: TextDocumentContentChangeEvent) { + if (!this.check(activeEditor, event)) { + return null; + } + + const lang: string = activeEditor.document.languageId; + let parser: CodeParser; + + switch (lang) { + case "cpp": + parser = new CppParser(); + break; + default: + // tslint:disable-next-line:no-console + console.log("No comments can be generated for language: " + lang); + return null; + } + parser.Parse(activeEditor, event).GenerateDoc(); + } +} diff --git a/src/CodeParser/CppParser.ts b/src/CodeParser/CppParser.ts new file mode 100644 index 0000000..e194371 --- /dev/null +++ b/src/CodeParser/CppParser.ts @@ -0,0 +1,131 @@ +import { Position, TextDocumentContentChangeEvent, TextEditor, TextLine } from "vscode"; +import CppGenerator from "../DocGen/CppGen"; +import { IDocGen } from "../DocGen/DocGen"; +import ICodeParser from "./CodeParser"; + +/** + * + * Parses C++ code for methods and signatures + * + * @export + * @class CppParser + * @implements {ICodeParser} + */ +export default class CppParser implements ICodeParser { + private activeEditor: TextEditor; + private activeSelection: Position; + + /** + * @inheritdoc + */ + public Parse(activeEdit: TextEditor, event: TextDocumentContentChangeEvent): IDocGen { + this.activeEditor = activeEdit; + this.activeSelection = this.activeEditor.selection.active; + + const activeLine: TextLine = this.activeEditor.document.lineAt(this.activeEditor.selection.active.line); + + const method: string = this.getMethodText(); + + // Not a method + if (method.length === 0) { + return null; + } + + const returnValue: string[] = this.getReturn(method); + + const params: string[] = this.getParams(method); + + const cppGenerator: IDocGen = new CppGenerator(this.activeEditor, this.activeSelection, params, returnValue); + return cppGenerator; + } + + /*************************************************************************** + Implementation + ***************************************************************************/ + + private getMethodText(): string { + let method: string = ""; + + let nextLine: Position = new Position(this.activeSelection.line + 1, this.activeSelection.character); + + let nextLineTxt: string = this.activeEditor.document.lineAt(nextLine.line).text.trim(); + + // VSCode may enter a * on itself, we don't want that in our method + if (nextLineTxt === "*") { + nextLineTxt = ""; + } + + while (nextLineTxt.length === 0) { // Get first method line + nextLine = new Position(nextLine.line + 1, nextLine.character); + nextLineTxt = this.activeEditor.document.lineAt(nextLine.line).text.trim(); + } + + method += nextLineTxt; + + // Get method end line + while (nextLineTxt.indexOf(")") === -1 && + (nextLineTxt.indexOf(";") === -1 || nextLineTxt.indexOf("}") === -1)) { // Check for method end + nextLine = new Position(nextLine.line + 1, nextLine.character); + nextLineTxt = this.activeEditor.document.lineAt(nextLine.line).text.trim(); + + method += " " + nextLineTxt; + } + + // Not a method but some code in the file + if (method.indexOf(")") === -1) { + return ""; + } + + return method; + } + + private getReturn(method: string): string[] { + const retVals: string[] = []; + + // Remove the parameters from the signature + const returnSignature: string = method.slice(0, method.indexOf("(")).trim(); + + if (returnSignature.indexOf(" ") === -1) { // Constructor or similar + return retVals; + } + + const returnType: string = returnSignature.substr(0, returnSignature.lastIndexOf(" ")); + + switch (returnType) { + case "bool": + retVals.push("true"); + retVals.push("false"); + break; + case "void": + break; + default: + retVals.push(returnType); + break; + } + + return retVals; + } + + private getParams(method: string): string[] { + const params: string[] = []; + + // Get parameters from enclosing brackets + const parameters: string = method.slice(method.indexOf("(")) // Get opening bracket + .slice(1, method.indexOf(")")) // Remove opening bracket + .split(")")[0]; // Get closing bracket + + if (parameters.length === 0) { // No parameters + return params; + } + + let paramArr: string[] = parameters.split(","); + paramArr = paramArr.map((item: string) => { + // Remove any special C++ characters + const clean: string = item.trim().replace(/[&*\[\]]/g, ""); + + return clean.split(" ").pop(); + }); + + return paramArr; + } +} |