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

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

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