diff options
| author | Rowan Goemans <RB.Goemans@student.han.nl> | 2017-12-31 02:03:08 +0100 |
|---|---|---|
| committer | Christoph Schlosser <christophschlosser@users.noreply.github.com> | 2018-02-20 22:02:22 +0100 |
| commit | 1746f21bc3720585d2fe877db8d69aae301ff02c (patch) | |
| tree | 7f0d081fd8fc3e0b54c7bb478cb8724a4cf3b798 /src/CodeParserController.ts | |
| parent | b446b9e596fc0487b88597d8e818dae61512f309 (diff) | |
| download | doxdocgen-1746f21bc3720585d2fe877db8d69aae301ff02c.tar.gz | |
-- Added many unit tests for the C parser. Almost complete now
-- Refactored project structure to allow for more overview once we add more languages.
-- Renamed a lot of types in the CParser to be more explicit.
-- Made configuration injectable into the classes that need them. This allows to unit test configuration variables.
-- Added 2 new config params that allows to customize what gets returned for a bool return type.
-- Small changes to the logic to make it more concise.
Diffstat (limited to 'src/CodeParserController.ts')
| -rw-r--r-- | src/CodeParserController.ts | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/CodeParserController.ts b/src/CodeParserController.ts new file mode 100644 index 0000000..32d1d75 --- /dev/null +++ b/src/CodeParserController.ts @@ -0,0 +1,108 @@ +import { + Disposable, + Position, + Range, + TextDocumentContentChangeEvent, + TextEditor, + TextLine, + window, + workspace, +} from "vscode"; +import CodeParser from "./Common/ICodeParser"; +import { Config } from "./Config"; +import CParser from "./Lang/C/CParser"; +/** + * + * 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 cfg: Config; + + /** + * Creates an instance of CodeParserController + * + * @memberOf CodeParserController + */ + public constructor() { + const subscriptions: Disposable[] = []; + + // 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.cfg = Config.ImportFromSettings(); + 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 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(); + + return this.cfg.triggerSequence === cont; + } + + 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 "c": + case "cpp": + parser = new CParser(this.cfg); + break; + default: + // tslint:disable-next-line:no-console + console.log("No comments can be generated for language: " + lang); + return null; + } + + const currentPos: Position = window.activeTextEditor.selection.active; + const startReplace: Position = new Position( + currentPos.line, + currentPos.character - this.cfg.triggerSequence.length, + ); + + const nextLineText: string = window.activeTextEditor.document.lineAt(startReplace.line + 1).text; + const endReplace = new Position(currentPos.line + 1, nextLineText.length); + + parser.Parse(activeEditor).GenerateDoc(new Range(startReplace, endReplace)); + } +} |