提交 0614806c 编写于 作者: J Johannes Rieken

move out util-methods to make MarkedString API ready, #29076

上级 018d5a2b
......@@ -15,26 +15,6 @@ export interface IMarkdownString {
export class MarkdownString implements IMarkdownString {
static isEmpty(oneOrMany: IMarkdownString | IMarkdownString[]): boolean {
if (MarkdownString.isMarkdownString(oneOrMany)) {
return !oneOrMany.value;
} else if (Array.isArray(oneOrMany)) {
return oneOrMany.every(MarkdownString.isEmpty);
} else {
return false;
}
}
static isMarkdownString(thing: any): thing is IMarkdownString {
if (thing instanceof MarkdownString) {
return true;
} else if (typeof thing === 'object') {
return typeof (<IMarkdownString>thing).value === 'string'
&& (typeof (<IMarkdownString>thing).trusted === 'boolean' || (<IMarkdownString>thing).trusted === void 0);
}
return false;
}
value: string;
trusted?: true;
......@@ -42,13 +22,18 @@ export class MarkdownString implements IMarkdownString {
this.value = value;
}
appendText(value: string): this {
appendText(value: string): MarkdownString {
// escape markdown syntax tokens: http://daringfireball.net/projects/markdown/syntax#backslash
this.value += value.replace(/[\\`*_{}[\]()#+\-.!]/g, '\\$&');
return this;
}
appendCodeblock(langId: string, code: string): this {
appendMarkdown(value: string): MarkdownString {
this.value += value;
return this;
}
appendCodeblock(langId: string, code: string): MarkdownString {
this.value += '\n```';
this.value += langId;
this.value += '\n';
......@@ -58,6 +43,26 @@ export class MarkdownString implements IMarkdownString {
}
}
export function isEmptyMarkdownString(oneOrMany: IMarkdownString | IMarkdownString[]): boolean {
if (isMarkdownString(oneOrMany)) {
return !oneOrMany.value;
} else if (Array.isArray(oneOrMany)) {
return oneOrMany.every(isEmptyMarkdownString);
} else {
return true;
}
}
export function isMarkdownString(thing: any): thing is IMarkdownString {
if (thing instanceof MarkdownString) {
return true;
} else if (typeof thing === 'object') {
return typeof (<IMarkdownString>thing).value === 'string'
&& (typeof (<IMarkdownString>thing).trusted === 'boolean' || (<IMarkdownString>thing).trusted === void 0);
}
return false;
}
export function markedStringsEquals(a: IMarkdownString | IMarkdownString[], b: IMarkdownString | IMarkdownString[]): boolean {
if (!a && !b) {
return true;
......@@ -65,7 +70,7 @@ export function markedStringsEquals(a: IMarkdownString | IMarkdownString[], b: I
return false;
} else if (Array.isArray(a) && Array.isArray(b)) {
return equals(a, b, markdownStringEqual);
} else if (MarkdownString.isMarkdownString(a) && MarkdownString.isMarkdownString(b)) {
} else if (isMarkdownString(a) && isMarkdownString(b)) {
return markdownStringEqual(a, b);
} else {
return false;
......
......@@ -20,7 +20,7 @@ import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { getHover } from '../common/hover';
import { HoverOperation, IHoverComputer } from './hoverOperation';
import { ContentHoverWidget } from './hoverWidgets';
import { IMarkdownString, MarkdownString } from 'vs/base/common/htmlContent';
import { IMarkdownString, MarkdownString, isEmptyMarkdownString } from 'vs/base/common/htmlContent';
import { ModelDecorationOptions } from 'vs/editor/common/model/textModelWithDecorations';
import { ColorPickerModel } from 'vs/editor/contrib/colorPicker/browser/colorPickerModel';
import { ColorPickerWidget } from 'vs/editor/contrib/colorPicker/browser/colorPickerWidget';
......@@ -103,7 +103,7 @@ class ModesContentComputer implements IHoverComputer<HoverPart[]> {
const { color, formatters } = colorRange;
return new ColorHover(d.range, color, formatters);
} else {
if (MarkdownString.isEmpty(d.options.hoverMessage)) {
if (isEmptyMarkdownString(d.options.hoverMessage)) {
return null;
}
......@@ -310,7 +310,7 @@ export class ModesContentHoverWidget extends ContentHoverWidget {
if (!(msg instanceof ColorHover)) {
msg.contents
.filter(contents => !MarkdownString.isEmpty(contents))
.filter(contents => !isEmptyMarkdownString(contents))
.forEach(contents => {
const renderedContents = renderMarkdown(contents, {
actionCallback: (content) => {
......
......@@ -15,7 +15,7 @@ import { onUnexpectedError } from 'vs/base/common/errors';
import { TPromise } from 'vs/base/common/winjs.base';
import { IModeService } from 'vs/editor/common/services/modeService';
import { tokenizeToString } from 'vs/editor/common/modes/textToHtmlTokenizer';
import { IMarkdownString, MarkdownString } from 'vs/base/common/htmlContent';
import { IMarkdownString, isEmptyMarkdownString } from 'vs/base/common/htmlContent';
export interface IHoverMessage {
value: IMarkdownString;
......@@ -61,7 +61,7 @@ class MarginComputer implements IHoverComputer<IHoverMessage[]> {
let hoverMessage = d.options.glyphMarginHoverMessage;
if (!MarkdownString.isEmpty(hoverMessage)) {
if (!isEmptyMarkdownString(hoverMessage)) {
continue;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册