Skip to content

Commit

Permalink
make internal variant of MarkedString be a string, #29076
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Aug 22, 2017
1 parent f3bf53f commit beb033f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 61 deletions.
8 changes: 1 addition & 7 deletions src/vs/base/browser/htmlContentRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ export function renderMarkedString(markedString: MarkedString, options: RenderOp
// this is sort of legacy given that we have full
// support for markdown. Turn this into markdown
// and continue
let markdown: string;
if (typeof markedString === 'string') {
markdown = markedString;
} else {
markdown = '```' + markedString.language + '\n' + markedString.value + '\n```';
}
return renderMarkdown(markdown, options);
return renderMarkdown(markedString, options);
}

export function renderText(text: string, options: RenderOptions = {}): Node {
Expand Down
52 changes: 9 additions & 43 deletions src/vs/base/common/htmlContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,27 @@

'use strict';

import { equals } from 'vs/base/common/arrays';

/**
* MarkedString can be used to render human readable text. It is either a markdown string
* or a code-block that provides a language and a code snippet. Note that
* markdown strings will be sanitized - that means html will be escaped.
*/
export type MarkedString = string | { readonly language: string; readonly value: string };
export type MarkedString = string;

export function markedStringsEquals(a: MarkedString | MarkedString[], b: MarkedString | MarkedString[]): boolean {
if (!a && !b) {
return true;
}
if (!a || !b) {
} else if (!a || !b) {
return false;
}

if (Array.isArray(a)) {
if (!Array.isArray(b)) {
return false;
}
return markedStringArrEquals(a, b);
}
return markedStringEqual(a, b as MarkedString);
}


function markedStringArrEquals(a: MarkedString[], b: MarkedString[]): boolean {
let aLen = a.length,
bLen = b.length;

if (aLen !== bLen) {
} else if (typeof a === 'string' && typeof b === 'string') {
return a === b;
} else if (Array.isArray(a) && Array.isArray(b)) {
return equals(a, b);
} else {
return false;
}

for (let i = 0; i < aLen; i++) {
if (!markedStringEqual(a[i], b[i])) {
return false;
}
}

return true;
}
function markedStringEqual(a: MarkedString, b: MarkedString): boolean {
if (!a && !b) {
return true;
}
if (!a || !b) {
return false;
}
if (typeof a === 'string' || typeof b === 'string') {
return typeof a === 'string' && typeof b === 'string' && a === b;
}
return (
a.language === b.language
&& a.value === b.value
);
}

export function textToMarkedString(text: string): MarkedString {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/common/services/modelServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class ModelMarkerHandler {
}
}

hoverMessage = [{ language: '_', value: message }];
hoverMessage = ['```_\n' + message + '\n```'];
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
const previewRange = new Range(startLineNumber, 1, endLineNumber + 1, 1);
const value = textEditorModel.getValueInRange(previewRange).replace(new RegExp(`^\\s{${minIndent - 1}}`, 'gm'), '').trim();

this.addDecoration(new Range(position.lineNumber, word.startColumn, position.lineNumber, word.endColumn), {
language: this.modeService.getModeIdByFilenameOrFirstLine(textEditorModel.uri.fsPath),
value
});
this.addDecoration(
new Range(position.lineNumber, word.startColumn, position.lineNumber, word.endColumn),
'```' + this.modeService.getModeIdByFilenameOrFirstLine(textEditorModel.uri.fsPath) + '\n' + value + '\n```'
);
ref.dispose();
});
}
Expand Down
5 changes: 1 addition & 4 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,7 @@ declare module monaco {
* or a code-block that provides a language and a code snippet. Note that
* markdown strings will be sanitized - that means html will be escaped.
*/
export type MarkedString = string | {
readonly language: string;
readonly value: string;
};
export type MarkedString = string;

export interface IKeyboardEvent {
readonly browserEvent: KeyboardEvent;
Expand Down
15 changes: 13 additions & 2 deletions src/vs/workbench/api/node/extHostTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,23 @@ function isDecorationOptionsArr(something: vscode.Range[] | vscode.DecorationOpt
return isDecorationOptions(something[0]) ? true : false;
}

export namespace MarkedString {
export function from(markup: vscode.MarkedString): string {
if (typeof markup === 'string' || !markup) {
return <string>markup;
} else {
const { language, value } = markup;
return '```' + language + '\n' + value + '\n```';
}
}
}

export function fromRangeOrRangeWithMessage(ranges: vscode.Range[] | vscode.DecorationOptions[]): IDecorationOptions[] {
if (isDecorationOptionsArr(ranges)) {
return ranges.map((r): IDecorationOptions => {
return {
range: fromRange(r.range),
hoverMessage: r.hoverMessage,
hoverMessage: Array.isArray(r.hoverMessage) ? r.hoverMessage.map(MarkedString.from) : MarkedString.from(r.hoverMessage),
renderOptions: <any> /* URI vs Uri */r.renderOptions
};
});
Expand Down Expand Up @@ -256,7 +267,7 @@ export const location = {
export function fromHover(hover: vscode.Hover): modes.Hover {
return <modes.Hover>{
range: fromRange(hover.range),
contents: hover.contents
contents: hover.contents.map(MarkedString.from)
};
}

Expand Down

0 comments on commit beb033f

Please sign in to comment.