From beb033f98b6464cdba39457bcddb4d5aea644671 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Tue, 22 Aug 2017 12:00:56 +0200 Subject: [PATCH] make internal variant of MarkedString be a string, #29076 --- src/vs/base/browser/htmlContentRenderer.ts | 8 +-- src/vs/base/common/htmlContent.ts | 52 ++++--------------- .../common/services/modelServiceImpl.ts | 2 +- .../browser/goToDeclarationMouse.ts | 8 +-- src/vs/monaco.d.ts | 5 +- .../api/node/extHostTypeConverters.ts | 15 +++++- 6 files changed, 29 insertions(+), 61 deletions(-) diff --git a/src/vs/base/browser/htmlContentRenderer.ts b/src/vs/base/browser/htmlContentRenderer.ts index 70c5972557c6c..6dd423dbec168 100644 --- a/src/vs/base/browser/htmlContentRenderer.ts +++ b/src/vs/base/browser/htmlContentRenderer.ts @@ -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 { diff --git a/src/vs/base/common/htmlContent.ts b/src/vs/base/common/htmlContent.ts index 4744495a169a3..5f2f9fbeba463 100644 --- a/src/vs/base/common/htmlContent.ts +++ b/src/vs/base/common/htmlContent.ts @@ -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 { diff --git a/src/vs/editor/common/services/modelServiceImpl.ts b/src/vs/editor/common/services/modelServiceImpl.ts index 24040eb3e5467..14e90ee2cfbbb 100644 --- a/src/vs/editor/common/services/modelServiceImpl.ts +++ b/src/vs/editor/common/services/modelServiceImpl.ts @@ -150,7 +150,7 @@ class ModelMarkerHandler { } } - hoverMessage = [{ language: '_', value: message }]; + hoverMessage = ['```_\n' + message + '\n```']; } return { diff --git a/src/vs/editor/contrib/goToDeclaration/browser/goToDeclarationMouse.ts b/src/vs/editor/contrib/goToDeclaration/browser/goToDeclarationMouse.ts index 6a8718e202b4c..f78d1b59cef23 100644 --- a/src/vs/editor/contrib/goToDeclaration/browser/goToDeclarationMouse.ts +++ b/src/vs/editor/contrib/goToDeclaration/browser/goToDeclarationMouse.ts @@ -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(); }); } diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index 97c8a13a94d56..bdc79c091abaa 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -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; diff --git a/src/vs/workbench/api/node/extHostTypeConverters.ts b/src/vs/workbench/api/node/extHostTypeConverters.ts index 863538775937e..a0ea9970792ba 100644 --- a/src/vs/workbench/api/node/extHostTypeConverters.ts +++ b/src/vs/workbench/api/node/extHostTypeConverters.ts @@ -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 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: /* URI vs Uri */r.renderOptions }; }); @@ -256,7 +267,7 @@ export const location = { export function fromHover(hover: vscode.Hover): modes.Hover { return { range: fromRange(hover.range), - contents: hover.contents + contents: hover.contents.map(MarkedString.from) }; }