From df68a8da1ac44ba701d6386a7f7bdf6d9c524d3b Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Wed, 10 Oct 2018 14:22:39 +0300 Subject: [PATCH] #2892 fix cyclic dependencies Signed-off-by: Yevhen Vydolob --- .../plugin-ext/src/plugin/markdown-string.ts | 56 +++++++++++++++++++ .../plugin-ext/src/plugin/plugin-context.ts | 2 +- .../src/plugin/type-converters.spec.ts | 15 ++--- .../plugin-ext/src/plugin/type-converters.ts | 12 +--- packages/plugin-ext/src/plugin/types-impl.ts | 32 +---------- 5 files changed, 67 insertions(+), 50 deletions(-) create mode 100644 packages/plugin-ext/src/plugin/markdown-string.ts diff --git a/packages/plugin-ext/src/plugin/markdown-string.ts b/packages/plugin-ext/src/plugin/markdown-string.ts new file mode 100644 index 0000000000000..cedf7a3090843 --- /dev/null +++ b/packages/plugin-ext/src/plugin/markdown-string.ts @@ -0,0 +1,56 @@ +/******************************************************************************** + * Copyright (C) 2018 Red Hat, Inc. and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the Eclipse + * Public License v. 2.0 are satisfied: GNU General Public License, version 2 + * with the GNU Classpath Exception which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + ********************************************************************************/ + +export class MarkdownString { + + value: string; + isTrusted?: boolean; + + constructor(value?: string) { + this.value = value || ''; + } + + appendText(value: string): MarkdownString { + // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash + this.value += value.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&'); + return this; + } + + appendMarkdown(value: string): MarkdownString { + this.value += value; + return this; + } + + appendCodeblock(code: string, language: string = ''): MarkdownString { + this.value += '\n```'; + this.value += language; + this.value += '\n'; + this.value += code; + this.value += '\n```\n'; + return this; + } +} + +// tslint:disable-next-line:no-any +export function isMarkdownString(thing: any): thing is MarkdownString { + if (thing instanceof MarkdownString) { + return true; + } else if (thing && typeof thing === 'object') { + return typeof (thing).value === 'string' + && (typeof (thing).isTrusted === 'boolean' || (thing).isTrusted === void 0); + } + return false; +} diff --git a/packages/plugin-ext/src/plugin/plugin-context.ts b/packages/plugin-ext/src/plugin/plugin-context.ts index 06842ab2c0f8e..748788c9a349b 100644 --- a/packages/plugin-ext/src/plugin/plugin-context.ts +++ b/packages/plugin-ext/src/plugin/plugin-context.ts @@ -36,7 +36,6 @@ import { TextEditorSelectionChangeKind, EndOfLine, SnippetString, - MarkdownString, ThemeColor, TextEditorRevealType, TextEditorLineNumbersStyle, @@ -77,6 +76,7 @@ import { TerminalServiceExtImpl } from './terminal-ext'; import { LanguagesExtImpl, score } from './languages'; import { fromDocumentSelector } from './type-converters'; import { DialogsExtImpl } from './dialogs'; +import { MarkdownString } from './markdown-string'; export function createAPIFactory(rpc: RPCProtocol, pluginManager: PluginManager): PluginAPIFactory { const commandRegistryExt = rpc.set(MAIN_RPC_CONTEXT.COMMAND_REGISTRY_EXT, new CommandRegistryImpl(rpc)); diff --git a/packages/plugin-ext/src/plugin/type-converters.spec.ts b/packages/plugin-ext/src/plugin/type-converters.spec.ts index e641b149aa9bf..f1b0a814b4887 100644 --- a/packages/plugin-ext/src/plugin/type-converters.spec.ts +++ b/packages/plugin-ext/src/plugin/type-converters.spec.ts @@ -19,6 +19,7 @@ import * as Converter from './type-converters'; import * as theia from '@theia/plugin'; import * as types from './types-impl'; import * as model from '../api/model'; +import { MarkdownString, isMarkdownString } from './markdown-string'; describe('Type converters:', () => { @@ -57,10 +58,10 @@ describe('Type converters:', () => { it('should recognize markdown string', () => { // given - const markdownString = new types.MarkdownString('**test**'); + const markdownString = new MarkdownString('**test**'); // when - const result = Converter.isMarkdownString(markdownString); + const result = isMarkdownString(markdownString); // then assert.deepEqual(result !== false, true); @@ -71,7 +72,7 @@ describe('Type converters:', () => { const markdownObject = { value: '*test*' }; // when - const result = Converter.isMarkdownString(markdownObject); + const result = isMarkdownString(markdownObject); // then assert.deepEqual(result !== false, true); @@ -82,7 +83,7 @@ describe('Type converters:', () => { const markdownObject = { field1: 5, value: '*test*', field2: 'test' }; // when - const result = Converter.isMarkdownString(markdownObject); + const result = isMarkdownString(markdownObject); // then assert.deepEqual(result !== false, true); @@ -93,7 +94,7 @@ describe('Type converters:', () => { const nonMarkdownObject = { field1: 5, field2: 'test' }; // when - const result = Converter.isMarkdownString(nonMarkdownObject); + const result = isMarkdownString(nonMarkdownObject); // then assert.deepEqual(result === false, true); @@ -104,7 +105,7 @@ describe('Type converters:', () => { const nonMarkdownObject = { isTrusted: true, field1: 5, field2: 'test' }; // when - const result = Converter.isMarkdownString(nonMarkdownObject); + const result = isMarkdownString(nonMarkdownObject); // then assert.deepEqual(result === false, true); @@ -113,7 +114,7 @@ describe('Type converters:', () => { describe('converter: ', () => { const aStringWithMarkdown: string = '**test**'; - const pluginMarkdown: theia.MarkdownString = new types.MarkdownString(aStringWithMarkdown); + const pluginMarkdown: theia.MarkdownString = new MarkdownString(aStringWithMarkdown); const aLanguage = 'typescript'; const aValue = 'const x=5;'; const codeblock = { language: aLanguage, value: aValue }; diff --git a/packages/plugin-ext/src/plugin/type-converters.ts b/packages/plugin-ext/src/plugin/type-converters.ts index baf1bdb505638..1ed43f2e5c483 100644 --- a/packages/plugin-ext/src/plugin/type-converters.ts +++ b/packages/plugin-ext/src/plugin/type-converters.ts @@ -30,6 +30,7 @@ import { import * as theia from '@theia/plugin'; import * as types from './types-impl'; import { LanguageSelector, LanguageFilter, RelativePattern } from './languages'; +import { isMarkdownString } from './markdown-string'; export function toViewColumn(ep?: EditorPosition): theia.ViewColumn | undefined { if (typeof ep !== 'number') { @@ -148,17 +149,6 @@ function isCodeblock(thing: any): thing is Codeblock { && typeof (thing).value === 'string'; } -// tslint:disable-next-line:no-any -export function isMarkdownString(thing: any): thing is MarkdownString { - if (thing instanceof types.MarkdownString) { - return true; - } else if (thing && typeof thing === 'object') { - return typeof (thing).value === 'string' - && (typeof (thing).isTrusted === 'boolean' || (thing).isTrusted === void 0); - } - return false; -} - export function fromMarkdown(markup: theia.MarkdownString | theia.MarkedString): MarkdownString { if (isCodeblock(markup)) { const { language, value } = markup; diff --git a/packages/plugin-ext/src/plugin/types-impl.ts b/packages/plugin-ext/src/plugin/types-impl.ts index b0b4e78811971..1e1d0fa2f5ebd 100644 --- a/packages/plugin-ext/src/plugin/types-impl.ts +++ b/packages/plugin-ext/src/plugin/types-impl.ts @@ -18,8 +18,8 @@ import { illegalArgument } from '../common/errors'; import * as theia from '@theia/plugin'; import URI from 'vscode-uri'; import { relative } from '../common/paths-util'; -import { isMarkdownString } from './type-converters'; import { startsWithIgnoreCase } from '../common/strings'; +import { MarkdownString, isMarkdownString } from './markdown-string'; export class Disposable { private disposable: undefined | (() => void); @@ -511,36 +511,6 @@ export class SnippetString { } } -export class MarkdownString { - - value: string; - isTrusted?: boolean; - - constructor(value?: string) { - this.value = value || ''; - } - - appendText(value: string): MarkdownString { - // escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash - this.value += value.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&'); - return this; - } - - appendMarkdown(value: string): MarkdownString { - this.value += value; - return this; - } - - appendCodeblock(code: string, language: string = ''): MarkdownString { - this.value += '\n```'; - this.value += language; - this.value += '\n'; - this.value += code; - this.value += '\n```\n'; - return this; - } -} - export class ThemeColor { constructor(public id: string) { }