Add MarkdownString to Hover and Decorations API, #29076

This commit is contained in:
Johannes Rieken
2017-08-25 11:44:54 +02:00
parent 3ab2688b17
commit 52ac8ab367
4 changed files with 75 additions and 20 deletions

View File

@@ -52,6 +52,7 @@ import { TextEditorCursorStyle } from 'vs/editor/common/config/editorOptions';
import { ExtHostThreadService } from 'vs/workbench/services/thread/node/extHostThreadService';
import { ProxyIdentifier } from 'vs/workbench/services/thread/common/threadService';
import { ExtHostDialogs } from 'vs/workbench/api/node/extHostDialogs';
import { MarkdownString } from 'vs/base/common/htmlContent';
export interface IExtensionApiFactory {
(extension: IExtensionDescription): typeof vscode;
@@ -554,6 +555,7 @@ export function createApiFactory(
Hover: extHostTypes.Hover,
IndentAction: languageConfiguration.IndentAction,
Location: extHostTypes.Location,
MarkdownString: MarkdownString,
OverviewRulerLane: EditorCommon.OverviewRulerLane,
ParameterInformation: extHostTypes.ParameterInformation,
Position: extHostTypes.Position,

View File

@@ -16,7 +16,7 @@ import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
import { IPosition } from 'vs/editor/common/core/position';
import { IRange } from 'vs/editor/common/core/range';
import { ISelection } from 'vs/editor/common/core/selection';
import { IMarkdownString } from 'vs/base/common/htmlContent';
import * as htmlContent from 'vs/base/common/htmlContent';
export interface PositionLike {
line: number;
@@ -144,26 +144,35 @@ function isDecorationOptionsArr(something: vscode.Range[] | vscode.DecorationOpt
return isDecorationOptions(something[0]) ? true : false;
}
export namespace MarkedString {
export function from(markup: vscode.MarkedString): IMarkdownString {
if (typeof markup === 'string' || !markup) {
return { value: <string>markup || '', trusted: true };
export namespace MarkdownString {
export function fromMany(markup: (vscode.MarkdownString | vscode.MarkedString)[]): htmlContent.IMarkdownString[] {
return markup.map(MarkdownString.from);
}
export function from(markup: vscode.MarkdownString | vscode.MarkedString): htmlContent.IMarkdownString {
if (htmlContent.isMarkdownString(markup)) {
return markup;
} else if (typeof markup === 'string' || !markup) {
return { value: <string>markup || '', isTrusted: true };
} else {
const { language, value } = markup;
return { value: '```' + language + '\n' + value + '\n```' };
}
}
export function to(value: IMarkdownString): vscode.MarkedString {
return value.value;
export function to(value: htmlContent.IMarkdownString): vscode.MarkdownString {
const ret = new htmlContent.MarkdownString(value.value);
ret.isTrusted = value.isTrusted;
return ret;
}
}
export function fromRangeOrRangeWithMessage(ranges: vscode.Range[] | vscode.DecorationOptions[]): IDecorationOptions[] {
if (isDecorationOptionsArr(ranges)) {
return ranges.map((r): IDecorationOptions => {
return ranges.map(r => {
return {
range: fromRange(r.range),
hoverMessage: Array.isArray(r.hoverMessage) ? r.hoverMessage.map(MarkedString.from) : MarkedString.from(r.hoverMessage),
hoverMessage: Array.isArray(r.hoverMessage) ? MarkdownString.fromMany(r.hoverMessage) : r.hoverMessage && MarkdownString.from(r.hoverMessage),
renderOptions: <any> /* URI vs Uri */r.renderOptions
};
});
@@ -271,12 +280,12 @@ export const location = {
export function fromHover(hover: vscode.Hover): modes.Hover {
return <modes.Hover>{
range: fromRange(hover.range),
contents: hover.contents.map(MarkedString.from)
contents: MarkdownString.fromMany(hover.contents)
};
}
export function toHover(info: modes.Hover): types.Hover {
return new types.Hover(info.contents.map(MarkedString.to), toRange(info.range));
return new types.Hover(info.contents.map(MarkdownString.to), toRange(info.range));
}
export function toDocumentHighlight(occurrence: modes.DocumentHighlight): types.DocumentHighlight {

View File

@@ -10,6 +10,7 @@ import URI from 'vs/base/common/uri';
import { Color as BaseColor, HSLA } from 'vs/base/common/color';
import { illegalArgument } from 'vs/base/common/errors';
import * as vscode from 'vscode';
import { isMarkdownString } from 'vs/base/common/htmlContent';
export class Disposable {
@@ -698,16 +699,20 @@ export class Diagnostic {
export class Hover {
public contents: vscode.MarkedString[];
public contents: vscode.MarkdownString[] | vscode.MarkedString[];
public range: Range;
constructor(contents: vscode.MarkedString | vscode.MarkedString[], range?: Range) {
constructor(
contents: vscode.MarkdownString | vscode.MarkedString | vscode.MarkdownString[] | vscode.MarkedString[],
range?: Range
) {
if (!contents) {
throw new Error('Illegal argument, contents must be defined');
}
if (Array.isArray(contents)) {
this.contents = contents;
this.contents = <vscode.MarkdownString[] | vscode.MarkedString[]>contents;
} else if (isMarkdownString(contents)) {
this.contents = [contents];
} else {
this.contents = [contents];
}
@@ -1377,4 +1382,4 @@ export enum ConfigurationTarget {
Workspace = 2,
WorkspaceFolder = 3
}
}