From d360dc99596a6869417739617a2f9b7052b11316 Mon Sep 17 00:00:00 2001 From: Nikolaj Kappler Date: Mon, 5 Nov 2018 11:25:49 +0100 Subject: [PATCH 1/2] [CodeMirror] add type defs for comment addon. workstream: --- types/codemirror/codemirror-comment.d.ts | 47 ++++++++++++++++++++++++ types/codemirror/test/comment.ts | 16 ++++++++ types/codemirror/tsconfig.json | 4 +- 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 types/codemirror/codemirror-comment.d.ts create mode 100644 types/codemirror/test/comment.ts diff --git a/types/codemirror/codemirror-comment.d.ts b/types/codemirror/codemirror-comment.d.ts new file mode 100644 index 0000000000..50c964ba3d --- /dev/null +++ b/types/codemirror/codemirror-comment.d.ts @@ -0,0 +1,47 @@ +// Type definitions for CodeMirror +// Project: https://github.com/marijnh/CodeMirror +// Definitions by: Nikolaj Kappler +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +// See docs https://codemirror.net/doc/manual.html#addon_comment + +// Todo: add 'toggleComment' command, once command type definitions exist in main definitions + +import * as CodeMirror from "codemirror"; + +declare module "codemirror" { + + interface Editor { + /** Tries to uncomment the current selection, and if that fails, line-comments it. */ + toggleComment(from: CodeMirror.Position, to: CodeMirror.Position, options?: CommentOptions): void; + /** Set the lines in the given range to be line comments. Will fall back to `blockComment` when no line comment style is defined for the mode. */ + lineComment(from: CodeMirror.Position, to: CodeMirror.Position, options?: CommentOptions): void; + /** Wrap the code in the given range in a block comment. Will fall back to `lineComment` when no block comment style is defined for the mode. */ + blockComment(from: CodeMirror.Position, to: CodeMirror.Position, options?: CommentOptions): void; + /** Try to uncomment the given range. Returns `true` if a comment range was found and removed, `false` otherwise. */ + uncomment(from: CodeMirror.Position, to: CodeMirror.Position, options?: CommentOptions): boolean; + } + + interface CommentOptions { + /** Override the [comment string properties](https://codemirror.net/doc/manual.html#mode_comment) of the mode with custom comment strings. */ + blockCommentStart?: string; + /** Override the [comment string properties](https://codemirror.net/doc/manual.html#mode_comment) of the mode with custom comment strings. */ + blockCommentEnd?: string; + /** Override the [comment string properties](https://codemirror.net/doc/manual.html#mode_comment) of the mode with custom comment strings. */ + blockCommentLead?: string; + /** Override the [comment string properties](https://codemirror.net/doc/manual.html#mode_comment) of the mode with custom comment strings. */ + lineComment?: string; + /** A string that will be inserted after opening and leading markers, and before closing comment markers. Defaults to a single space. */ + padding?: string; + /** Whether, when adding line comments, to also comment lines that contain only whitespace. */ + commentBlankLines?: boolean; + /** When adding line comments and this is turned on, it will align the comment block to the current indentation of the first line of the block. */ + indent?: boolean; + /** When block commenting, this controls whether the whole lines are indented, or only the precise range that is given. Defaults to `true`. */ + fullLines?: boolean; + } +} + +declare module "codemirror/addon/comment/comment" { + export = CodeMirror; +} diff --git a/types/codemirror/test/comment.ts b/types/codemirror/test/comment.ts new file mode 100644 index 0000000000..3f0fb0b663 --- /dev/null +++ b/types/codemirror/test/comment.ts @@ -0,0 +1,16 @@ +const editor = CodeMirror(document.body); +const position: CodeMirror.Position = { ch: 0, line: 0 }; +const opt: CodeMirror.CommentOptions = { + blockCommentEnd: "*/", + blockCommentStart: "/*", + blockCommentLead: "*", + commentBlankLines: true, + fullLines: true, + indent: true, + lineComment: "//", + padding: " " +}; +editor.toggleComment(position, position, opt); +editor.blockComment(position, position, opt); +editor.lineComment(position, position, opt); +const b: boolean = editor.uncomment(position, position, opt); diff --git a/types/codemirror/tsconfig.json b/types/codemirror/tsconfig.json index 79d2eae5c3..d081586f6e 100644 --- a/types/codemirror/tsconfig.json +++ b/types/codemirror/tsconfig.json @@ -23,12 +23,14 @@ "codemirror-panel.d.ts", "codemirror-runmode.d.ts", "codemirror-showhint.d.ts", + "codemirror-comment.d.ts", "searchcursor.d.ts", "test/index.ts", "test/matchbrackets.ts", "test/panel.ts", "test/runmode.ts", "test/searchcursor.ts", - "test/showhint.ts" + "test/showhint.ts", + "test/comment.ts" ] } \ No newline at end of file From 9dc89bd8cb9ae1b604f00fea8b819580895f4168 Mon Sep 17 00:00:00 2001 From: Nikolaj Kappler Date: Mon, 5 Nov 2018 14:46:05 +0100 Subject: [PATCH 2/2] fix unecessary 'CodeMirror.' prefix for Position Interface workstream: --- types/codemirror/codemirror-comment.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/codemirror/codemirror-comment.d.ts b/types/codemirror/codemirror-comment.d.ts index 50c964ba3d..e92935ef00 100644 --- a/types/codemirror/codemirror-comment.d.ts +++ b/types/codemirror/codemirror-comment.d.ts @@ -13,13 +13,13 @@ declare module "codemirror" { interface Editor { /** Tries to uncomment the current selection, and if that fails, line-comments it. */ - toggleComment(from: CodeMirror.Position, to: CodeMirror.Position, options?: CommentOptions): void; + toggleComment(from: Position, to: Position, options?: CommentOptions): void; /** Set the lines in the given range to be line comments. Will fall back to `blockComment` when no line comment style is defined for the mode. */ - lineComment(from: CodeMirror.Position, to: CodeMirror.Position, options?: CommentOptions): void; + lineComment(from: Position, to: Position, options?: CommentOptions): void; /** Wrap the code in the given range in a block comment. Will fall back to `lineComment` when no block comment style is defined for the mode. */ - blockComment(from: CodeMirror.Position, to: CodeMirror.Position, options?: CommentOptions): void; + blockComment(from: Position, to: Position, options?: CommentOptions): void; /** Try to uncomment the given range. Returns `true` if a comment range was found and removed, `false` otherwise. */ - uncomment(from: CodeMirror.Position, to: CodeMirror.Position, options?: CommentOptions): boolean; + uncomment(from: Position, to: Position, options?: CommentOptions): boolean; } interface CommentOptions {