Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plug-in] Fix cyclic dependencies #3127

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions packages/plugin-ext/src/plugin/markdown-string.ts
Original file line number Diff line number Diff line change
@@ -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 (<MarkdownString>thing).value === 'string'
&& (typeof (<MarkdownString>thing).isTrusted === 'boolean' || (<MarkdownString>thing).isTrusted === void 0);
}
return false;
}
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
TextEditorSelectionChangeKind,
EndOfLine,
SnippetString,
MarkdownString,
ThemeColor,
TextEditorRevealType,
TextEditorLineNumbersStyle,
Expand Down Expand Up @@ -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));
Expand Down
15 changes: 8 additions & 7 deletions packages/plugin-ext/src/plugin/type-converters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:', () => {

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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 };
Expand Down
12 changes: 1 addition & 11 deletions packages/plugin-ext/src/plugin/type-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -148,17 +149,6 @@ function isCodeblock(thing: any): thing is Codeblock {
&& typeof (<Codeblock>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 (<MarkdownString>thing).value === 'string'
&& (typeof (<MarkdownString>thing).isTrusted === 'boolean' || (<MarkdownString>thing).isTrusted === void 0);
}
return false;
}

export function fromMarkdown(markup: theia.MarkdownString | theia.MarkedString): MarkdownString {
if (isCodeblock(markup)) {
const { language, value } = markup;
Expand Down
32 changes: 1 addition & 31 deletions packages/plugin-ext/src/plugin/types-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
}
Expand Down