extHostTypeConverters.ts 44.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import * as modes from 'vs/editor/common/modes';
7
import * as types from './extHostTypes';
8
import * as search from 'vs/workbench/contrib/search/common/search';
9
import { ITextEditorOptions } from 'vs/platform/editor/common/editor';
10
import { EditorViewColumn } from 'vs/workbench/api/common/shared/editor';
A
Alex Dima 已提交
11 12
import { IDecorationOptions, IThemeDecorationRenderOptions, IDecorationRenderOptions, IContentDecorationRenderOptions } from 'vs/editor/common/editorCommon';
import { EndOfLineSequence, TrackedRangeStickiness } from 'vs/editor/common/model';
13
import type * as vscode from 'vscode';
J
Johannes Rieken 已提交
14
import { URI, UriComponents } from 'vs/base/common/uri';
15
import { ProgressLocation as MainProgressLocation } from 'vs/platform/progress/common/progress';
16
import { SaveReason } from 'vs/workbench/common/editor';
17
import { IPosition } from 'vs/editor/common/core/position';
J
Johannes Rieken 已提交
18
import * as editorRange from 'vs/editor/common/core/range';
19
import { ISelection } from 'vs/editor/common/core/selection';
20
import * as htmlContent from 'vs/base/common/htmlContent';
21
import * as languageSelector from 'vs/editor/common/modes/languageSelector';
22
import * as extHostProtocol from 'vs/workbench/api/common/extHost.protocol';
23
import { MarkerSeverity, IRelatedInformation, IMarkerData, MarkerTag } from 'vs/platform/markers/common/markers';
24
import { ACTIVE_GROUP, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
J
Johannes Rieken 已提交
25
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
S
Sandeep Somavarapu 已提交
26
import { isString, isNumber } from 'vs/base/common/types';
J
Johannes Rieken 已提交
27
import * as marked from 'vs/base/common/marked/marked';
J
Johannes Rieken 已提交
28 29
import { parse } from 'vs/base/common/marshalling';
import { cloneAndChange } from 'vs/base/common/objects';
30
import { LogLevel as _MainLogLevel } from 'vs/platform/log/common/log';
31
import { coalesce, isNonEmptyArray } from 'vs/base/common/arrays';
32
import { RenderLineNumbersType } from 'vs/editor/common/config/editorOptions';
33
import { CommandsConverter } from 'vs/workbench/api/common/extHostCommands';
E
Erich Gamma 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

export interface PositionLike {
	line: number;
	character: number;
}

export interface RangeLike {
	start: PositionLike;
	end: PositionLike;
}

export interface SelectionLike extends RangeLike {
	anchor: PositionLike;
	active: PositionLike;
}
49
export namespace Selection {
E
Erich Gamma 已提交
50

51
	export function to(selection: ISelection): types.Selection {
M
Matt Bierner 已提交
52 53 54
		const { selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn } = selection;
		const start = new types.Position(selectionStartLineNumber - 1, selectionStartColumn - 1);
		const end = new types.Position(positionLineNumber - 1, positionColumn - 1);
55 56
		return new types.Selection(start, end);
	}
E
Erich Gamma 已提交
57

58
	export function from(selection: SelectionLike): ISelection {
M
Matt Bierner 已提交
59
		const { anchor, active } = selection;
60 61 62 63 64 65 66
		return {
			selectionStartLineNumber: anchor.line + 1,
			selectionStartColumn: anchor.character + 1,
			positionLineNumber: active.line + 1,
			positionColumn: active.character + 1
		};
	}
E
Erich Gamma 已提交
67
}
68
export namespace Range {
E
Erich Gamma 已提交
69

70
	export function from(range: undefined): undefined;
J
Johannes Rieken 已提交
71 72 73
	export function from(range: RangeLike): editorRange.IRange;
	export function from(range: RangeLike | undefined): editorRange.IRange | undefined;
	export function from(range: RangeLike | undefined): editorRange.IRange | undefined {
74 75 76
		if (!range) {
			return undefined;
		}
M
Matt Bierner 已提交
77
		const { start, end } = range;
78 79 80 81 82 83
		return {
			startLineNumber: start.line + 1,
			startColumn: start.character + 1,
			endLineNumber: end.line + 1,
			endColumn: end.character + 1
		};
J
Johannes Rieken 已提交
84
	}
E
Erich Gamma 已提交
85

86
	export function to(range: undefined): types.Range;
J
Johannes Rieken 已提交
87 88 89
	export function to(range: editorRange.IRange): types.Range;
	export function to(range: editorRange.IRange | undefined): types.Range | undefined;
	export function to(range: editorRange.IRange | undefined): types.Range | undefined {
90 91 92
		if (!range) {
			return undefined;
		}
M
Matt Bierner 已提交
93
		const { startLineNumber, startColumn, endLineNumber, endColumn } = range;
94
		return new types.Range(startLineNumber - 1, startColumn - 1, endLineNumber - 1, endColumn - 1);
J
Johannes Rieken 已提交
95
	}
E
Erich Gamma 已提交
96 97
}

98 99 100 101 102 103 104 105 106 107 108
export namespace TokenType {
	export function to(type: modes.StandardTokenType): types.StandardTokenType {
		switch (type) {
			case modes.StandardTokenType.Comment: return types.StandardTokenType.Comment;
			case modes.StandardTokenType.Other: return types.StandardTokenType.Other;
			case modes.StandardTokenType.RegEx: return types.StandardTokenType.RegEx;
			case modes.StandardTokenType.String: return types.StandardTokenType.String;
		}
	}
}

109 110 111 112
export namespace Position {
	export function to(position: IPosition): types.Position {
		return new types.Position(position.lineNumber - 1, position.column - 1);
	}
113
	export function from(position: types.Position | vscode.Position): IPosition {
114 115
		return { lineNumber: position.line + 1, column: position.character + 1 };
	}
116 117
}

118
export namespace DiagnosticTag {
119
	export function from(value: vscode.DiagnosticTag): MarkerTag | undefined {
120 121 122
		switch (value) {
			case types.DiagnosticTag.Unnecessary:
				return MarkerTag.Unnecessary;
123 124
			case types.DiagnosticTag.Deprecated:
				return MarkerTag.Deprecated;
125 126 127
		}
		return undefined;
	}
J
Johannes Rieken 已提交
128 129 130 131 132 133
	export function to(value: MarkerTag): vscode.DiagnosticTag | undefined {
		switch (value) {
			case MarkerTag.Unnecessary:
				return types.DiagnosticTag.Unnecessary;
			case MarkerTag.Deprecated:
				return types.DiagnosticTag.Deprecated;
M
mtaran-google 已提交
134 135
			default:
				return undefined;
J
Johannes Rieken 已提交
136 137
		}
	}
138 139
}

140 141
export namespace Diagnostic {
	export function from(value: vscode.Diagnostic): IMarkerData {
P
Pine Wu 已提交
142 143 144 145 146 147 148 149 150 151 152
		let code: string | { value: string; target: URI } | undefined;

		if (value.code) {
			if (isString(value.code) || isNumber(value.code)) {
				code = String(value.code);
			} else {
				code = {
					value: String(value.code.value),
					target: value.code.target,
				};
			}
153 154
		}

155 156 157 158
		return {
			...Range.from(value.range),
			message: value.message,
			source: value.source,
159
			code,
160
			severity: DiagnosticSeverity.from(value.severity),
161
			relatedInformation: value.relatedInformation && value.relatedInformation.map(DiagnosticRelatedInformation.from),
162
			tags: Array.isArray(value.tags) ? coalesce(value.tags.map(DiagnosticTag.from)) : undefined,
163 164
		};
	}
J
Johannes Rieken 已提交
165 166 167 168

	export function to(value: IMarkerData): vscode.Diagnostic {
		const res = new types.Diagnostic(Range.to(value), value.message, DiagnosticSeverity.to(value.severity));
		res.source = value.source;
169
		res.code = isString(value.code) ? value.code : value.code?.value;
J
Johannes Rieken 已提交
170 171 172 173
		res.relatedInformation = value.relatedInformation && value.relatedInformation.map(DiagnosticRelatedInformation.to);
		res.tags = value.tags && coalesce(value.tags.map(DiagnosticTag.to));
		return res;
	}
174 175
}

176
export namespace DiagnosticRelatedInformation {
177
	export function from(value: vscode.DiagnosticRelatedInformation): IRelatedInformation {
178 179 180 181 182 183 184 185
		return {
			...Range.from(value.location.range),
			message: value.message,
			resource: value.location.uri
		};
	}
	export function to(value: IRelatedInformation): types.DiagnosticRelatedInformation {
		return new types.DiagnosticRelatedInformation(new types.Location(value.resource, Range.to(value)), value.message);
E
Erich Gamma 已提交
186 187
	}
}
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
export namespace DiagnosticSeverity {

	export function from(value: number): MarkerSeverity {
		switch (value) {
			case types.DiagnosticSeverity.Error:
				return MarkerSeverity.Error;
			case types.DiagnosticSeverity.Warning:
				return MarkerSeverity.Warning;
			case types.DiagnosticSeverity.Information:
				return MarkerSeverity.Info;
			case types.DiagnosticSeverity.Hint:
				return MarkerSeverity.Hint;
		}
		return MarkerSeverity.Error;
	}
E
Erich Gamma 已提交
203

204 205 206 207 208 209 210 211 212 213
	export function to(value: MarkerSeverity): types.DiagnosticSeverity {
		switch (value) {
			case MarkerSeverity.Info:
				return types.DiagnosticSeverity.Information;
			case MarkerSeverity.Warning:
				return types.DiagnosticSeverity.Warning;
			case MarkerSeverity.Error:
				return types.DiagnosticSeverity.Error;
			case MarkerSeverity.Hint:
				return types.DiagnosticSeverity.Hint;
M
mtaran-google 已提交
214 215
			default:
				return types.DiagnosticSeverity.Error;
216
		}
E
Erich Gamma 已提交
217 218 219
	}
}

220
export namespace ViewColumn {
221
	export function from(column?: vscode.ViewColumn): EditorViewColumn {
222 223
		if (typeof column === 'number' && column >= types.ViewColumn.One) {
			return column - 1; // adjust zero index (ViewColumn.ONE => 0)
224
		}
225 226 227 228 229 230

		if (column === types.ViewColumn.Beside) {
			return SIDE_GROUP;
		}

		return ACTIVE_GROUP; // default is always the active group
E
Erich Gamma 已提交
231 232
	}

M
Matt Bierner 已提交
233
	export function to(position: EditorViewColumn): vscode.ViewColumn {
234 235
		if (typeof position === 'number' && position >= 0) {
			return position + 1; // adjust to index (ViewColumn.ONE => 1)
236
		}
237

M
Matt Bierner 已提交
238
		throw new Error(`invalid 'EditorViewColumn'`);
239 240
	}
}
E
Erich Gamma 已提交
241

M
Martin Aeschlimann 已提交
242
function isDecorationOptions(something: any): something is vscode.DecorationOptions {
243
	return (typeof something.range !== 'undefined');
E
Erich Gamma 已提交
244 245
}

246
export function isDecorationOptionsArr(something: vscode.Range[] | vscode.DecorationOptions[]): something is vscode.DecorationOptions[] {
E
Erich Gamma 已提交
247 248 249
	if (something.length === 0) {
		return true;
	}
M
Martin Aeschlimann 已提交
250
	return isDecorationOptions(something[0]) ? true : false;
E
Erich Gamma 已提交
251 252
}

253 254 255 256 257 258
export namespace MarkdownString {

	export function fromMany(markup: (vscode.MarkdownString | vscode.MarkedString)[]): htmlContent.IMarkdownString[] {
		return markup.map(MarkdownString.from);
	}

J
Johannes Rieken 已提交
259 260 261 262 263 264
	interface Codeblock {
		language: string;
		value: string;
	}

	function isCodeblock(thing: any): thing is Codeblock {
J
Johannes Rieken 已提交
265
		return thing && typeof thing === 'object'
J
Johannes Rieken 已提交
266 267 268 269
			&& typeof (<Codeblock>thing).language === 'string'
			&& typeof (<Codeblock>thing).value === 'string';
	}

270
	export function from(markup: vscode.MarkdownString | vscode.MarkedString): htmlContent.IMarkdownString {
271
		let res: htmlContent.IMarkdownString;
J
Johannes Rieken 已提交
272 273
		if (isCodeblock(markup)) {
			const { language, value } = markup;
274
			res = { value: '```' + language + '\n' + value + '\n```\n' };
J
Johannes Rieken 已提交
275
		} else if (htmlContent.isMarkdownString(markup)) {
276
			res = markup;
J
Johannes Rieken 已提交
277
		} else if (typeof markup === 'string') {
M
Matt Bierner 已提交
278
			res = { value: markup };
279
		} else {
280
			res = { value: '' };
281
		}
J
Johannes Rieken 已提交
282

283
		// extract uris into a separate object
284
		const resUris: { [href: string]: UriComponents; } = Object.create(null);
285 286
		res.uris = resUris;

287
		const collectUri = (href: string): string => {
J
Johannes Rieken 已提交
288
			try {
289
				let uri = URI.parse(href, true);
290 291
				uri = uri.with({ query: _uriMassage(uri.query, resUris) });
				resUris[href] = uri;
J
Johannes Rieken 已提交
292 293 294 295 296
			} catch (e) {
				// ignore
			}
			return '';
		};
297 298 299 300
		const renderer = new marked.Renderer();
		renderer.link = collectUri;
		renderer.image = href => collectUri(htmlContent.parseHrefAndDimensions(href).href);

301 302 303
		marked(res.value, { renderer });

		return res;
J
Johannes Rieken 已提交
304 305
	}

306
	function _uriMassage(part: string, bucket: { [n: string]: UriComponents; }): string {
J
Johannes Rieken 已提交
307 308 309 310 311
		if (!part) {
			return part;
		}
		let data: any;
		try {
J
Johannes Rieken 已提交
312
			data = parse(part);
J
Johannes Rieken 已提交
313 314 315 316 317 318
		} catch (e) {
			// ignore
		}
		if (!data) {
			return part;
		}
319
		let changed = false;
J
Johannes Rieken 已提交
320
		data = cloneAndChange(data, value => {
321
			if (URI.isUri(value)) {
322
				const key = `__uri_${Math.random().toString(16).slice(2, 8)}`;
J
Johannes Rieken 已提交
323
				bucket[key] = value;
324
				changed = true;
J
Johannes Rieken 已提交
325 326 327 328 329
				return key;
			} else {
				return undefined;
			}
		});
330 331 332 333 334 335

		if (!changed) {
			return part;
		}

		return JSON.stringify(data);
J
Johannes Rieken 已提交
336 337
	}

338
	export function to(value: htmlContent.IMarkdownString): vscode.MarkdownString {
E
Eric Amodio 已提交
339
		return new htmlContent.MarkdownString(value.value, { isTrusted: value.isTrusted, supportThemeIcons: value.supportThemeIcons });
340
	}
341 342 343 344 345 346 347

	export function fromStrict(value: string | types.MarkdownString): undefined | string | htmlContent.IMarkdownString {
		if (!value) {
			return undefined;
		}
		return typeof value === 'string' ? value : MarkdownString.from(value);
	}
348 349
}

350
export function fromRangeOrRangeWithMessage(ranges: vscode.Range[] | vscode.DecorationOptions[]): IDecorationOptions[] {
M
Martin Aeschlimann 已提交
351
	if (isDecorationOptionsArr(ranges)) {
352
		return ranges.map((r): IDecorationOptions => {
E
Erich Gamma 已提交
353
			return {
354
				range: Range.from(r.range),
355 356 357
				hoverMessage: Array.isArray(r.hoverMessage)
					? MarkdownString.fromMany(r.hoverMessage)
					: (r.hoverMessage ? MarkdownString.from(r.hoverMessage) : undefined),
358
				renderOptions: <any> /* URI vs Uri */r.renderOptions
E
Erich Gamma 已提交
359 360 361
			};
		});
	} else {
M
Martin Aeschlimann 已提交
362
		return ranges.map((r): IDecorationOptions => {
E
Erich Gamma 已提交
363
			return {
364
				range: Range.from(r)
B
Benjamin Pasero 已提交
365
			};
E
Erich Gamma 已提交
366 367 368
		});
	}
}
369

P
Peng Lyu 已提交
370
export function pathOrURIToURI(value: string | URI): URI {
A
Alex Dima 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	if (typeof value === 'undefined') {
		return value;
	}
	if (typeof value === 'string') {
		return URI.file(value);
	} else {
		return value;
	}
}

export namespace ThemableDecorationAttachmentRenderOptions {
	export function from(options: vscode.ThemableDecorationAttachmentRenderOptions): IContentDecorationRenderOptions {
		if (typeof options === 'undefined') {
			return options;
		}
		return {
			contentText: options.contentText,
388
			contentIconPath: options.contentIconPath ? pathOrURIToURI(options.contentIconPath) : undefined,
A
Alex Dima 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
			border: options.border,
			borderColor: <string | types.ThemeColor>options.borderColor,
			fontStyle: options.fontStyle,
			fontWeight: options.fontWeight,
			textDecoration: options.textDecoration,
			color: <string | types.ThemeColor>options.color,
			backgroundColor: <string | types.ThemeColor>options.backgroundColor,
			margin: options.margin,
			width: options.width,
			height: options.height,
		};
	}
}

export namespace ThemableDecorationRenderOptions {
	export function from(options: vscode.ThemableDecorationRenderOptions): IThemeDecorationRenderOptions {
		if (typeof options === 'undefined') {
			return options;
		}
		return {
			backgroundColor: <string | types.ThemeColor>options.backgroundColor,
			outline: options.outline,
			outlineColor: <string | types.ThemeColor>options.outlineColor,
			outlineStyle: options.outlineStyle,
			outlineWidth: options.outlineWidth,
			border: options.border,
			borderColor: <string | types.ThemeColor>options.borderColor,
			borderRadius: options.borderRadius,
			borderSpacing: options.borderSpacing,
			borderStyle: options.borderStyle,
			borderWidth: options.borderWidth,
			fontStyle: options.fontStyle,
			fontWeight: options.fontWeight,
			textDecoration: options.textDecoration,
			cursor: options.cursor,
			color: <string | types.ThemeColor>options.color,
			opacity: options.opacity,
			letterSpacing: options.letterSpacing,
427
			gutterIconPath: options.gutterIconPath ? pathOrURIToURI(options.gutterIconPath) : undefined,
A
Alex Dima 已提交
428 429
			gutterIconSize: options.gutterIconSize,
			overviewRulerColor: <string | types.ThemeColor>options.overviewRulerColor,
430 431
			before: options.before ? ThemableDecorationAttachmentRenderOptions.from(options.before) : undefined,
			after: options.after ? ThemableDecorationAttachmentRenderOptions.from(options.after) : undefined,
A
Alex Dima 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
		};
	}
}

export namespace DecorationRangeBehavior {
	export function from(value: types.DecorationRangeBehavior): TrackedRangeStickiness {
		if (typeof value === 'undefined') {
			return value;
		}
		switch (value) {
			case types.DecorationRangeBehavior.OpenOpen:
				return TrackedRangeStickiness.AlwaysGrowsWhenTypingAtEdges;
			case types.DecorationRangeBehavior.ClosedClosed:
				return TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges;
			case types.DecorationRangeBehavior.OpenClosed:
				return TrackedRangeStickiness.GrowsOnlyWhenTypingBefore;
			case types.DecorationRangeBehavior.ClosedOpen:
				return TrackedRangeStickiness.GrowsOnlyWhenTypingAfter;
		}
	}
}

export namespace DecorationRenderOptions {
	export function from(options: vscode.DecorationRenderOptions): IDecorationRenderOptions {
		return {
			isWholeLine: options.isWholeLine,
458
			rangeBehavior: options.rangeBehavior ? DecorationRangeBehavior.from(options.rangeBehavior) : undefined,
A
Alex Dima 已提交
459
			overviewRulerLane: options.overviewRulerLane,
460 461
			light: options.light ? ThemableDecorationRenderOptions.from(options.light) : undefined,
			dark: options.dark ? ThemableDecorationRenderOptions.from(options.dark) : undefined,
A
Alex Dima 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

			backgroundColor: <string | types.ThemeColor>options.backgroundColor,
			outline: options.outline,
			outlineColor: <string | types.ThemeColor>options.outlineColor,
			outlineStyle: options.outlineStyle,
			outlineWidth: options.outlineWidth,
			border: options.border,
			borderColor: <string | types.ThemeColor>options.borderColor,
			borderRadius: options.borderRadius,
			borderSpacing: options.borderSpacing,
			borderStyle: options.borderStyle,
			borderWidth: options.borderWidth,
			fontStyle: options.fontStyle,
			fontWeight: options.fontWeight,
			textDecoration: options.textDecoration,
			cursor: options.cursor,
			color: <string | types.ThemeColor>options.color,
			opacity: options.opacity,
			letterSpacing: options.letterSpacing,
481
			gutterIconPath: options.gutterIconPath ? pathOrURIToURI(options.gutterIconPath) : undefined,
A
Alex Dima 已提交
482 483
			gutterIconSize: options.gutterIconSize,
			overviewRulerColor: <string | types.ThemeColor>options.overviewRulerColor,
484 485
			before: options.before ? ThemableDecorationAttachmentRenderOptions.from(options.before) : undefined,
			after: options.after ? ThemableDecorationAttachmentRenderOptions.from(options.after) : undefined,
A
Alex Dima 已提交
486 487 488 489
		};
	}
}

M
Matt Bierner 已提交
490
export namespace TextEdit {
491

M
Matt Bierner 已提交
492
	export function from(edit: vscode.TextEdit): modes.TextEdit {
J
Johannes Rieken 已提交
493
		return <modes.TextEdit>{
494
			text: edit.newText,
J
Johannes Rieken 已提交
495
			eol: edit.newEol && EndOfLine.from(edit.newEol),
496
			range: Range.from(edit.range)
B
Benjamin Pasero 已提交
497
		};
M
Matt Bierner 已提交
498 499 500
	}

	export function to(edit: modes.TextEdit): types.TextEdit {
M
Matt Bierner 已提交
501
		const result = new types.TextEdit(Range.to(edit.range), edit.text);
M
Matt Bierner 已提交
502
		result.newEol = (typeof edit.eol === 'undefined' ? undefined : EndOfLine.to(edit.eol))!;
J
Johannes Rieken 已提交
503
		return result;
504
	}
M
Matt Bierner 已提交
505
}
506

507
export namespace WorkspaceEdit {
508 509
	export function from(value: vscode.WorkspaceEdit, documents?: ExtHostDocumentsAndEditors): extHostProtocol.IWorkspaceEditDto {
		const result: extHostProtocol.IWorkspaceEditDto = {
510
			edits: []
511
		};
512 513

		if (value instanceof types.WorkspaceEdit) {
514
			for (let entry of value._allEntries()) {
515

516
				if (entry._type === types.FileEditType.File) {
517 518
					// file operation
					result.edits.push(<extHostProtocol.IWorkspaceFileEditDto>{
519
						_type: extHostProtocol.WorkspaceEditType.File,
520 521 522 523 524 525
						oldUri: entry.from,
						newUri: entry.to,
						options: entry.options,
						metadata: entry.metadata
					});

526
				} else if (entry._type === types.FileEditType.Text) {
527 528 529
					// text edits
					const doc = documents?.getDocument(entry.uri);
					result.edits.push(<extHostProtocol.IWorkspaceTextEditDto>{
530
						_type: extHostProtocol.WorkspaceEditType.Text,
531 532 533 534 535
						resource: entry.uri,
						edit: TextEdit.from(entry.edit),
						modelVersionId: doc?.version,
						metadata: entry.metadata
					});
536 537 538 539 540 541 542 543
				} else if (entry._type === types.FileEditType.Cell) {
					result.edits.push(<extHostProtocol.IWorkspaceCellEditDto>{
						_type: extHostProtocol.WorkspaceEditType.Cell,
						resource: entry.uri,
						edit: entry.edit,
						metadata: entry.metadata,
						modelVersionId: undefined, // todo@jrieken
					});
544
				}
545 546 547 548 549
			}
		}
		return result;
	}

550
	export function to(value: extHostProtocol.IWorkspaceEditDto) {
551 552
		const result = new types.WorkspaceEdit();
		for (const edit of value.edits) {
553 554 555 556 557
			if ((<extHostProtocol.IWorkspaceTextEditDto>edit).edit) {
				result.replace(
					URI.revive((<extHostProtocol.IWorkspaceTextEditDto>edit).resource),
					Range.to((<extHostProtocol.IWorkspaceTextEditDto>edit).edit.range),
					(<extHostProtocol.IWorkspaceTextEditDto>edit).edit.text
558
				);
J
Johannes Rieken 已提交
559 560
			} else {
				result.renameFile(
561 562 563
					URI.revive((<extHostProtocol.IWorkspaceFileEditDto>edit).oldUri!),
					URI.revive((<extHostProtocol.IWorkspaceFileEditDto>edit).newUri!),
					(<extHostProtocol.IWorkspaceFileEditDto>edit).options
J
Johannes Rieken 已提交
564
				);
565
			}
566 567 568
		}
		return result;
	}
569 570
}

571

572 573
export namespace SymbolKind {

574
	const _fromMapping: { [kind: number]: modes.SymbolKind; } = Object.create(null);
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
	_fromMapping[types.SymbolKind.File] = modes.SymbolKind.File;
	_fromMapping[types.SymbolKind.Module] = modes.SymbolKind.Module;
	_fromMapping[types.SymbolKind.Namespace] = modes.SymbolKind.Namespace;
	_fromMapping[types.SymbolKind.Package] = modes.SymbolKind.Package;
	_fromMapping[types.SymbolKind.Class] = modes.SymbolKind.Class;
	_fromMapping[types.SymbolKind.Method] = modes.SymbolKind.Method;
	_fromMapping[types.SymbolKind.Property] = modes.SymbolKind.Property;
	_fromMapping[types.SymbolKind.Field] = modes.SymbolKind.Field;
	_fromMapping[types.SymbolKind.Constructor] = modes.SymbolKind.Constructor;
	_fromMapping[types.SymbolKind.Enum] = modes.SymbolKind.Enum;
	_fromMapping[types.SymbolKind.Interface] = modes.SymbolKind.Interface;
	_fromMapping[types.SymbolKind.Function] = modes.SymbolKind.Function;
	_fromMapping[types.SymbolKind.Variable] = modes.SymbolKind.Variable;
	_fromMapping[types.SymbolKind.Constant] = modes.SymbolKind.Constant;
	_fromMapping[types.SymbolKind.String] = modes.SymbolKind.String;
	_fromMapping[types.SymbolKind.Number] = modes.SymbolKind.Number;
	_fromMapping[types.SymbolKind.Boolean] = modes.SymbolKind.Boolean;
	_fromMapping[types.SymbolKind.Array] = modes.SymbolKind.Array;
	_fromMapping[types.SymbolKind.Object] = modes.SymbolKind.Object;
	_fromMapping[types.SymbolKind.Key] = modes.SymbolKind.Key;
	_fromMapping[types.SymbolKind.Null] = modes.SymbolKind.Null;
	_fromMapping[types.SymbolKind.EnumMember] = modes.SymbolKind.EnumMember;
	_fromMapping[types.SymbolKind.Struct] = modes.SymbolKind.Struct;
598 599
	_fromMapping[types.SymbolKind.Event] = modes.SymbolKind.Event;
	_fromMapping[types.SymbolKind.Operator] = modes.SymbolKind.Operator;
600
	_fromMapping[types.SymbolKind.TypeParameter] = modes.SymbolKind.TypeParameter;
601 602

	export function from(kind: vscode.SymbolKind): modes.SymbolKind {
J
Johannes Rieken 已提交
603
		return typeof _fromMapping[kind] === 'number' ? _fromMapping[kind] : modes.SymbolKind.Property;
604 605
	}

606
	export function to(kind: modes.SymbolKind): vscode.SymbolKind {
M
Matt Bierner 已提交
607
		for (const k in _fromMapping) {
608 609
			if (_fromMapping[k] === kind) {
				return Number(k);
610
			}
611 612
		}
		return types.SymbolKind.Property;
613 614 615
	}
}

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
export namespace SymbolTag {

	export function from(kind: types.SymbolTag): modes.SymbolTag {
		switch (kind) {
			case types.SymbolTag.Deprecated: return modes.SymbolTag.Deprecated;
		}
	}

	export function to(kind: modes.SymbolTag): types.SymbolTag {
		switch (kind) {
			case modes.SymbolTag.Deprecated: return types.SymbolTag.Deprecated;
		}
	}
}

631 632 633
export namespace WorkspaceSymbol {
	export function from(info: vscode.SymbolInformation): search.IWorkspaceSymbol {
		return <search.IWorkspaceSymbol>{
634 635
			name: info.name,
			kind: SymbolKind.from(info.kind),
636
			tags: info.tags && info.tags.map(SymbolTag.from),
637 638 639 640
			containerName: info.containerName,
			location: location.from(info.location)
		};
	}
641
	export function to(info: search.IWorkspaceSymbol): types.SymbolInformation {
642
		const result = new types.SymbolInformation(
643 644 645 646 647
			info.name,
			SymbolKind.to(info.kind),
			info.containerName,
			location.to(info.location)
		);
648 649
		result.tags = info.tags && info.tags.map(SymbolTag.to);
		return result;
650
	}
651 652
}

653
export namespace DocumentSymbol {
654
	export function from(info: vscode.DocumentSymbol): modes.DocumentSymbol {
M
Matt Bierner 已提交
655
		const result: modes.DocumentSymbol = {
656
			name: info.name || '!!MISSING: name!!',
657
			detail: info.detail,
658 659
			range: Range.from(info.range),
			selectionRange: Range.from(info.selectionRange),
660
			kind: SymbolKind.from(info.kind),
661
			tags: info.tags?.map(SymbolTag.from) ?? []
662 663 664 665 666 667
		};
		if (info.children) {
			result.children = info.children.map(from);
		}
		return result;
	}
668
	export function to(info: modes.DocumentSymbol): vscode.DocumentSymbol {
M
Matt Bierner 已提交
669
		const result = new types.DocumentSymbol(
670
			info.name,
671
			info.detail,
J
Johannes Rieken 已提交
672
			SymbolKind.to(info.kind),
673 674
			Range.to(info.range),
			Range.to(info.selectionRange),
675
		);
676 677 678
		if (isNonEmptyArray(info.tags)) {
			result.tags = info.tags.map(SymbolTag.to);
		}
679
		if (info.children) {
680
			result.children = info.children.map(to) as any;
681 682 683
		}
		return result;
	}
684 685
}

686 687
export namespace CallHierarchyItem {

688 689
	export function to(item: extHostProtocol.ICallHierarchyItemDto): types.CallHierarchyItem {
		const result = new types.CallHierarchyItem(
690 691 692 693 694 695 696
			SymbolKind.to(item.kind),
			item.name,
			item.detail || '',
			URI.revive(item.uri),
			Range.to(item.range),
			Range.to(item.selectionRange)
		);
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721

		result._sessionId = item._sessionId;
		result._itemId = item._itemId;

		return result;
	}
}

export namespace CallHierarchyIncomingCall {

	export function to(item: extHostProtocol.IIncomingCallDto): types.CallHierarchyIncomingCall {
		return new types.CallHierarchyIncomingCall(
			CallHierarchyItem.to(item.from),
			item.fromRanges.map(r => Range.to(r))
		);
	}
}

export namespace CallHierarchyOutgoingCall {

	export function to(item: extHostProtocol.IOutgoingCallDto): types.CallHierarchyOutgoingCall {
		return new types.CallHierarchyOutgoingCall(
			CallHierarchyItem.to(item.to),
			item.fromRanges.map(r => Range.to(r))
		);
722 723 724 725
	}
}


M
Matt Bierner 已提交
726 727
export namespace location {
	export function from(value: vscode.Location): modes.Location {
728
		return {
729
			range: value.range && Range.from(value.range),
J
Johannes Rieken 已提交
730
			uri: value.uri
J
Johannes Rieken 已提交
731
		};
M
Matt Bierner 已提交
732 733 734
	}

	export function to(value: modes.Location): types.Location {
735
		return new types.Location(value.uri, Range.to(value.range));
736
	}
M
Matt Bierner 已提交
737
}
738

M
Matt Bierner 已提交
739
export namespace DefinitionLink {
J
Johannes Rieken 已提交
740
	export function from(value: vscode.Location | vscode.DefinitionLink): modes.LocationLink {
M
Matt Bierner 已提交
741 742 743
		const definitionLink = <vscode.DefinitionLink>value;
		const location = <vscode.Location>value;
		return {
J
Johannes Rieken 已提交
744
			originSelectionRange: definitionLink.originSelectionRange
M
Matt Bierner 已提交
745 746 747 748
				? Range.from(definitionLink.originSelectionRange)
				: undefined,
			uri: definitionLink.targetUri ? definitionLink.targetUri : location.uri,
			range: Range.from(definitionLink.targetRange ? definitionLink.targetRange : location.range),
J
Johannes Rieken 已提交
749
			targetSelectionRange: definitionLink.targetSelectionRange
M
Matt Bierner 已提交
750 751 752 753
				? Range.from(definitionLink.targetSelectionRange)
				: undefined,
		};
	}
754 755 756 757 758 759 760 761 762 763 764 765
	export function to(value: modes.LocationLink): vscode.LocationLink {
		return {
			targetUri: value.uri,
			targetRange: Range.to(value.range),
			targetSelectionRange: value.targetSelectionRange
				? Range.to(value.targetSelectionRange)
				: undefined,
			originSelectionRange: value.originSelectionRange
				? Range.to(value.originSelectionRange)
				: undefined
		};
	}
M
Matt Bierner 已提交
766 767
}

768 769 770 771 772 773 774
export namespace Hover {
	export function from(hover: vscode.Hover): modes.Hover {
		return <modes.Hover>{
			range: Range.from(hover.range),
			contents: MarkdownString.fromMany(hover.contents)
		};
	}
775

776 777 778
	export function to(info: modes.Hover): types.Hover {
		return new types.Hover(info.contents.map(MarkdownString.to), Range.to(info.range));
	}
779
}
780 781 782 783 784 785 786 787 788 789 790 791 792 793

export namespace EvaluatableExpression {
	export function from(expression: vscode.EvaluatableExpression): modes.EvaluatableExpression {
		return <modes.EvaluatableExpression>{
			range: Range.from(expression.range),
			expression: expression.expression
		};
	}

	export function to(info: modes.EvaluatableExpression): types.EvaluatableExpression {
		return new types.EvaluatableExpression(Range.to(info.range), info.expression);
	}
}

794 795 796 797 798 799 800 801 802 803
export namespace DocumentHighlight {
	export function from(documentHighlight: vscode.DocumentHighlight): modes.DocumentHighlight {
		return {
			range: Range.from(documentHighlight.range),
			kind: documentHighlight.kind
		};
	}
	export function to(occurrence: modes.DocumentHighlight): types.DocumentHighlight {
		return new types.DocumentHighlight(Range.to(occurrence.range), occurrence.kind);
	}
804 805
}

M
Matt Bierner 已提交
806
export namespace CompletionTriggerKind {
807
	export function to(kind: modes.CompletionTriggerKind) {
M
Matt Bierner 已提交
808
		switch (kind) {
809
			case modes.CompletionTriggerKind.TriggerCharacter:
M
Matt Bierner 已提交
810
				return types.CompletionTriggerKind.TriggerCharacter;
811
			case modes.CompletionTriggerKind.TriggerForIncompleteCompletions:
812
				return types.CompletionTriggerKind.TriggerForIncompleteCompletions;
813
			case modes.CompletionTriggerKind.Invoke:
M
Matt Bierner 已提交
814 815 816 817 818 819 820
			default:
				return types.CompletionTriggerKind.Invoke;
		}
	}
}

export namespace CompletionContext {
821
	export function to(context: modes.CompletionContext): types.CompletionContext {
M
Matt Bierner 已提交
822
		return {
823
			triggerKind: CompletionTriggerKind.to(context.triggerKind),
M
Matt Bierner 已提交
824 825 826 827 828
			triggerCharacter: context.triggerCharacter
		};
	}
}

829
export namespace CompletionItemTag {
830

831
	export function from(kind: types.CompletionItemTag): modes.CompletionItemTag {
832
		switch (kind) {
833
			case types.CompletionItemTag.Deprecated: return modes.CompletionItemTag.Deprecated;
834 835 836
		}
	}

837
	export function to(kind: modes.CompletionItemTag): types.CompletionItemTag {
838
		switch (kind) {
839
			case modes.CompletionItemTag.Deprecated: return types.CompletionItemTag.Deprecated;
840 841 842 843
		}
	}
}

M
Matt Bierner 已提交
844
export namespace CompletionItemKind {
J
Johannes Rieken 已提交
845

846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
	const _from = new Map<types.CompletionItemKind, modes.CompletionItemKind>([
		[types.CompletionItemKind.Method, modes.CompletionItemKind.Method],
		[types.CompletionItemKind.Function, modes.CompletionItemKind.Function],
		[types.CompletionItemKind.Constructor, modes.CompletionItemKind.Constructor],
		[types.CompletionItemKind.Field, modes.CompletionItemKind.Field],
		[types.CompletionItemKind.Variable, modes.CompletionItemKind.Variable],
		[types.CompletionItemKind.Class, modes.CompletionItemKind.Class],
		[types.CompletionItemKind.Interface, modes.CompletionItemKind.Interface],
		[types.CompletionItemKind.Struct, modes.CompletionItemKind.Struct],
		[types.CompletionItemKind.Module, modes.CompletionItemKind.Module],
		[types.CompletionItemKind.Property, modes.CompletionItemKind.Property],
		[types.CompletionItemKind.Unit, modes.CompletionItemKind.Unit],
		[types.CompletionItemKind.Value, modes.CompletionItemKind.Value],
		[types.CompletionItemKind.Constant, modes.CompletionItemKind.Constant],
		[types.CompletionItemKind.Enum, modes.CompletionItemKind.Enum],
		[types.CompletionItemKind.EnumMember, modes.CompletionItemKind.EnumMember],
		[types.CompletionItemKind.Keyword, modes.CompletionItemKind.Keyword],
		[types.CompletionItemKind.Snippet, modes.CompletionItemKind.Snippet],
		[types.CompletionItemKind.Text, modes.CompletionItemKind.Text],
		[types.CompletionItemKind.Color, modes.CompletionItemKind.Color],
		[types.CompletionItemKind.File, modes.CompletionItemKind.File],
		[types.CompletionItemKind.Reference, modes.CompletionItemKind.Reference],
		[types.CompletionItemKind.Folder, modes.CompletionItemKind.Folder],
		[types.CompletionItemKind.Event, modes.CompletionItemKind.Event],
		[types.CompletionItemKind.Operator, modes.CompletionItemKind.Operator],
		[types.CompletionItemKind.TypeParameter, modes.CompletionItemKind.TypeParameter],
		[types.CompletionItemKind.Issue, modes.CompletionItemKind.Issue],
		[types.CompletionItemKind.User, modes.CompletionItemKind.User],
	]);

	export function from(kind: types.CompletionItemKind): modes.CompletionItemKind {
		return _from.get(kind) ?? modes.CompletionItemKind.Property;
	}

	const _to = new Map<modes.CompletionItemKind, types.CompletionItemKind>([
		[modes.CompletionItemKind.Method, types.CompletionItemKind.Method],
		[modes.CompletionItemKind.Function, types.CompletionItemKind.Function],
		[modes.CompletionItemKind.Constructor, types.CompletionItemKind.Constructor],
		[modes.CompletionItemKind.Field, types.CompletionItemKind.Field],
		[modes.CompletionItemKind.Variable, types.CompletionItemKind.Variable],
		[modes.CompletionItemKind.Class, types.CompletionItemKind.Class],
		[modes.CompletionItemKind.Interface, types.CompletionItemKind.Interface],
		[modes.CompletionItemKind.Struct, types.CompletionItemKind.Struct],
		[modes.CompletionItemKind.Module, types.CompletionItemKind.Module],
		[modes.CompletionItemKind.Property, types.CompletionItemKind.Property],
		[modes.CompletionItemKind.Unit, types.CompletionItemKind.Unit],
		[modes.CompletionItemKind.Value, types.CompletionItemKind.Value],
		[modes.CompletionItemKind.Constant, types.CompletionItemKind.Constant],
		[modes.CompletionItemKind.Enum, types.CompletionItemKind.Enum],
		[modes.CompletionItemKind.EnumMember, types.CompletionItemKind.EnumMember],
		[modes.CompletionItemKind.Keyword, types.CompletionItemKind.Keyword],
		[modes.CompletionItemKind.Snippet, types.CompletionItemKind.Snippet],
		[modes.CompletionItemKind.Text, types.CompletionItemKind.Text],
		[modes.CompletionItemKind.Color, types.CompletionItemKind.Color],
		[modes.CompletionItemKind.File, types.CompletionItemKind.File],
		[modes.CompletionItemKind.Reference, types.CompletionItemKind.Reference],
		[modes.CompletionItemKind.Folder, types.CompletionItemKind.Folder],
		[modes.CompletionItemKind.Event, types.CompletionItemKind.Event],
		[modes.CompletionItemKind.Operator, types.CompletionItemKind.Operator],
		[modes.CompletionItemKind.TypeParameter, types.CompletionItemKind.TypeParameter],
		[modes.CompletionItemKind.User, types.CompletionItemKind.User],
		[modes.CompletionItemKind.Issue, types.CompletionItemKind.Issue],
	]);
J
Johannes Rieken 已提交
909

M
Matt Bierner 已提交
910
	export function to(kind: modes.CompletionItemKind): types.CompletionItemKind {
911
		return _to.get(kind) ?? types.CompletionItemKind.Property;
J
Johannes Rieken 已提交
912
	}
M
Matt Bierner 已提交
913
}
J
Johannes Rieken 已提交
914

915
export namespace CompletionItem {
916

917
	export function to(suggestion: modes.CompletionItem, converter?: CommandsConverter): types.CompletionItem {
P
label2  
Pine Wu 已提交
918 919

		const result = new types.CompletionItem(typeof suggestion.label === 'string' ? suggestion.label : suggestion.label.name);
P
Pine Wu 已提交
920 921 922 923
		if (typeof suggestion.label !== 'string') {
			result.label2 = suggestion.label;
		}

924
		result.insertText = suggestion.insertText;
925
		result.kind = CompletionItemKind.to(suggestion.kind);
926
		result.tags = suggestion.tags?.map(CompletionItemTag.to);
927
		result.detail = suggestion.detail;
928
		result.documentation = htmlContent.isMarkdownString(suggestion.documentation) ? MarkdownString.to(suggestion.documentation) : suggestion.documentation;
929 930
		result.sortText = suggestion.sortText;
		result.filterText = suggestion.filterText;
J
Johannes Rieken 已提交
931
		result.preselect = suggestion.preselect;
J
Johannes Rieken 已提交
932
		result.commitCharacters = suggestion.commitCharacters;
933 934 935 936 937 938 939 940

		// range
		if (editorRange.Range.isIRange(suggestion.range)) {
			result.range = Range.to(suggestion.range);
		} else if (typeof suggestion.range === 'object') {
			result.range = { inserting: Range.to(suggestion.range.insert), replacing: Range.to(suggestion.range.replace) };
		}

941
		result.keepWhitespace = typeof suggestion.insertTextRules === 'undefined' ? false : Boolean(suggestion.insertTextRules & modes.CompletionItemInsertTextRule.KeepWhitespace);
942
		// 'insertText'-logic
943
		if (typeof suggestion.insertTextRules !== 'undefined' && suggestion.insertTextRules & modes.CompletionItemInsertTextRule.InsertAsSnippet) {
944 945 946
			result.insertText = new types.SnippetString(suggestion.insertText);
		} else {
			result.insertText = suggestion.insertText;
J
Johannes Rieken 已提交
947
			result.textEdit = result.range instanceof types.Range ? new types.TextEdit(result.range, result.insertText) : undefined;
948
		}
949 950 951 952
		if (suggestion.additionalTextEdits && suggestion.additionalTextEdits.length > 0) {
			result.additionalTextEdits = suggestion.additionalTextEdits.map(e => TextEdit.to(e as modes.TextEdit));
		}
		result.command = converter && suggestion.command ? converter.fromInternal(suggestion.command) : undefined;
953

954 955
		return result;
	}
956
}
957

958 959 960 961
export namespace ParameterInformation {
	export function from(info: types.ParameterInformation): modes.ParameterInformation {
		return {
			label: info.label,
962
			documentation: info.documentation ? MarkdownString.fromStrict(info.documentation) : undefined
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
		};
	}
	export function to(info: modes.ParameterInformation): types.ParameterInformation {
		return {
			label: info.label,
			documentation: htmlContent.isMarkdownString(info.documentation) ? MarkdownString.to(info.documentation) : info.documentation
		};
	}
}

export namespace SignatureInformation {

	export function from(info: types.SignatureInformation): modes.SignatureInformation {
		return {
			label: info.label,
978
			documentation: info.documentation ? MarkdownString.fromStrict(info.documentation) : undefined,
979 980
			parameters: Array.isArray(info.parameters) ? info.parameters.map(ParameterInformation.from) : [],
			activeParameter: info.activeParameter,
981 982 983 984 985 986 987
		};
	}

	export function to(info: modes.SignatureInformation): types.SignatureInformation {
		return {
			label: info.label,
			documentation: htmlContent.isMarkdownString(info.documentation) ? MarkdownString.to(info.documentation) : info.documentation,
988 989
			parameters: Array.isArray(info.parameters) ? info.parameters.map(ParameterInformation.to) : [],
			activeParameter: info.activeParameter,
990 991 992
		};
	}
}
993

994 995
export namespace SignatureHelp {

996 997 998 999
	export function from(help: types.SignatureHelp): modes.SignatureHelp {
		return {
			activeSignature: help.activeSignature,
			activeParameter: help.activeParameter,
1000
			signatures: Array.isArray(help.signatures) ? help.signatures.map(SignatureInformation.from) : [],
1001
		};
1002 1003
	}

1004 1005 1006 1007
	export function to(help: modes.SignatureHelp): types.SignatureHelp {
		return {
			activeSignature: help.activeSignature,
			activeParameter: help.activeParameter,
1008
			signatures: Array.isArray(help.signatures) ? help.signatures.map(SignatureInformation.to) : [],
1009
		};
1010
	}
J
Johannes Rieken 已提交
1011 1012
}

J
Johannes Rieken 已提交
1013 1014
export namespace DocumentLink {

1015
	export function from(link: vscode.DocumentLink): modes.ILink {
J
Johannes Rieken 已提交
1016
		return {
1017
			range: Range.from(link.range),
1018 1019
			url: link.target,
			tooltip: link.tooltip
J
Johannes Rieken 已提交
1020 1021 1022
		};
	}

1023
	export function to(link: modes.ILink): vscode.DocumentLink {
M
Matt Bierner 已提交
1024
		let target: URI | undefined = undefined;
M
Martin Aeschlimann 已提交
1025 1026
		if (link.url) {
			try {
1027
				target = typeof link.url === 'string' ? URI.parse(link.url, true) : URI.revive(link.url);
M
Martin Aeschlimann 已提交
1028 1029 1030 1031 1032
			} catch (err) {
				// ignore
			}
		}
		return new types.DocumentLink(Range.to(link.range), target);
J
Johannes Rieken 已提交
1033 1034
	}
}
1035

1036
export namespace ColorPresentation {
1037
	export function to(colorPresentation: modes.IColorPresentation): types.ColorPresentation {
M
Matt Bierner 已提交
1038
		const cp = new types.ColorPresentation(colorPresentation.label);
1039 1040 1041 1042 1043 1044 1045
		if (colorPresentation.textEdit) {
			cp.textEdit = TextEdit.to(colorPresentation.textEdit);
		}
		if (colorPresentation.additionalTextEdits) {
			cp.additionalTextEdits = colorPresentation.additionalTextEdits.map(value => TextEdit.to(value));
		}
		return cp;
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	}

	export function from(colorPresentation: vscode.ColorPresentation): modes.IColorPresentation {
		return {
			label: colorPresentation.label,
			textEdit: colorPresentation.textEdit ? TextEdit.from(colorPresentation.textEdit) : undefined,
			additionalTextEdits: colorPresentation.additionalTextEdits ? colorPresentation.additionalTextEdits.map(value => TextEdit.from(value)) : undefined
		};
	}
}

1057 1058 1059 1060 1061 1062 1063 1064 1065
export namespace Color {
	export function to(c: [number, number, number, number]): types.Color {
		return new types.Color(c[0], c[1], c[2], c[3]);
	}
	export function from(color: types.Color): [number, number, number, number] {
		return [color.red, color.green, color.blue, color.alpha];
	}
}

1066 1067 1068

export namespace SelectionRange {
	export function from(obj: vscode.SelectionRange): modes.SelectionRange {
1069
		return { range: Range.from(obj.range) };
1070 1071 1072
	}

	export function to(obj: modes.SelectionRange): vscode.SelectionRange {
1073
		return new types.SelectionRange(Range.to(obj.range));
1074 1075 1076
	}
}

1077 1078 1079 1080 1081
export namespace TextDocumentSaveReason {

	export function to(reason: SaveReason): vscode.TextDocumentSaveReason {
		switch (reason) {
			case SaveReason.AUTO:
1082
				return types.TextDocumentSaveReason.AfterDelay;
1083
			case SaveReason.EXPLICIT:
1084
				return types.TextDocumentSaveReason.Manual;
1085 1086 1087 1088 1089
			case SaveReason.FOCUS_CHANGE:
			case SaveReason.WINDOW_CHANGE:
				return types.TextDocumentSaveReason.FocusOut;
		}
	}
1090
}
1091

1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
export namespace TextEditorLineNumbersStyle {
	export function from(style: vscode.TextEditorLineNumbersStyle): RenderLineNumbersType {
		switch (style) {
			case types.TextEditorLineNumbersStyle.Off:
				return RenderLineNumbersType.Off;
			case types.TextEditorLineNumbersStyle.Relative:
				return RenderLineNumbersType.Relative;
			case types.TextEditorLineNumbersStyle.On:
			default:
				return RenderLineNumbersType.On;
		}
	}
	export function to(style: RenderLineNumbersType): vscode.TextEditorLineNumbersStyle {
		switch (style) {
			case RenderLineNumbersType.Off:
				return types.TextEditorLineNumbersStyle.Off;
			case RenderLineNumbersType.Relative:
				return types.TextEditorLineNumbersStyle.Relative;
			case RenderLineNumbersType.On:
			default:
				return types.TextEditorLineNumbersStyle.On;
		}
	}
}

1117 1118
export namespace EndOfLine {

1119
	export function from(eol: vscode.EndOfLine): EndOfLineSequence | undefined {
1120 1121 1122 1123 1124 1125
		if (eol === types.EndOfLine.CRLF) {
			return EndOfLineSequence.CRLF;
		} else if (eol === types.EndOfLine.LF) {
			return EndOfLineSequence.LF;
		}
		return undefined;
1126
	}
J
Johannes Rieken 已提交
1127

1128
	export function to(eol: EndOfLineSequence): vscode.EndOfLine | undefined {
J
Johannes Rieken 已提交
1129 1130 1131 1132 1133 1134 1135
		if (eol === EndOfLineSequence.CRLF) {
			return types.EndOfLine.CRLF;
		} else if (eol === EndOfLineSequence.LF) {
			return types.EndOfLine.LF;
		}
		return undefined;
	}
1136 1137
}

J
Johannes Rieken 已提交
1138
export namespace ProgressLocation {
1139
	export function from(loc: vscode.ProgressLocation | { viewId: string }): MainProgressLocation | string {
E
Eric Amodio 已提交
1140 1141
		if (typeof loc === 'object') {
			return loc.viewId;
1142 1143
		}

J
Johannes Rieken 已提交
1144
		switch (loc) {
1145
			case types.ProgressLocation.SourceControl: return MainProgressLocation.Scm;
J
Johannes Rieken 已提交
1146
			case types.ProgressLocation.Window: return MainProgressLocation.Window;
1147
			case types.ProgressLocation.Notification: return MainProgressLocation.Notification;
J
Johannes Rieken 已提交
1148
		}
M
Matt Bierner 已提交
1149
		throw new Error(`Unknown 'ProgressLocation'`);
J
Johannes Rieken 已提交
1150 1151
	}
}
1152

1153
export namespace FoldingRange {
1154
	export function from(r: vscode.FoldingRange): modes.FoldingRange {
M
Matt Bierner 已提交
1155
		const range: modes.FoldingRange = { start: r.start + 1, end: r.end + 1 };
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
		if (r.kind) {
			range.kind = FoldingRangeKind.from(r.kind);
		}
		return range;
	}
}

export namespace FoldingRangeKind {
	export function from(kind: vscode.FoldingRangeKind | undefined): modes.FoldingRangeKind | undefined {
		if (kind) {
			switch (kind) {
				case types.FoldingRangeKind.Comment:
					return modes.FoldingRangeKind.Comment;
				case types.FoldingRangeKind.Imports:
					return modes.FoldingRangeKind.Imports;
				case types.FoldingRangeKind.Region:
					return modes.FoldingRangeKind.Region;
			}
		}
R
Rob Lourens 已提交
1175
		return undefined;
1176 1177 1178
	}
}

1179 1180 1181 1182 1183
export interface TextEditorOpenOptions extends vscode.TextDocumentShowOptions {
	background?: boolean;
}

export namespace TextEditorOpenOptions {
1184

1185
	export function from(options?: TextEditorOpenOptions): ITextEditorOptions | undefined {
1186 1187 1188
		if (options) {
			return {
				pinned: typeof options.preview === 'boolean' ? !options.preview : undefined,
1189
				inactive: options.background,
1190
				preserveFocus: options.preserveFocus,
1191 1192
				selection: typeof options.selection === 'object' ? Range.from(options.selection) : undefined,
			};
1193
		}
1194

1195
		return undefined;
1196 1197 1198 1199
	}

}

1200
export namespace GlobPattern {
1201

1202
	export function from(pattern: vscode.GlobPattern): string | types.RelativePattern;
1203
	export function from(pattern: undefined): undefined;
1204 1205 1206
	export function from(pattern: null): null;
	export function from(pattern: vscode.GlobPattern | undefined | null): string | types.RelativePattern | undefined | null;
	export function from(pattern: vscode.GlobPattern | undefined | null): string | types.RelativePattern | undefined | null {
1207 1208 1209 1210
		if (pattern instanceof types.RelativePattern) {
			return pattern;
		}

1211 1212 1213
		if (typeof pattern === 'string') {
			return pattern;
		}
1214

1215 1216 1217 1218 1219
		if (isRelativePattern(pattern)) {
			return new types.RelativePattern(pattern.base, pattern.pattern);
		}

		return pattern; // preserve `undefined` and `null`
1220 1221
	}

1222 1223 1224 1225
	function isRelativePattern(obj: any): obj is vscode.RelativePattern {
		const rp = obj as vscode.RelativePattern;
		return rp && typeof rp.base === 'string' && typeof rp.pattern === 'string';
	}
1226 1227
}

1228
export namespace LanguageSelector {
1229

M
Matt Bierner 已提交
1230 1231 1232
	export function from(selector: undefined): undefined;
	export function from(selector: vscode.DocumentSelector): languageSelector.LanguageSelector;
	export function from(selector: vscode.DocumentSelector | undefined): languageSelector.LanguageSelector | undefined;
M
Matt Bierner 已提交
1233
	export function from(selector: vscode.DocumentSelector | undefined): languageSelector.LanguageSelector | undefined {
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
		if (!selector) {
			return undefined;
		} else if (Array.isArray(selector)) {
			return <languageSelector.LanguageSelector>selector.map(from);
		} else if (typeof selector === 'string') {
			return selector;
		} else {
			return <languageSelector.LanguageFilter>{
				language: selector.language,
				scheme: selector.scheme,
M
Matt Bierner 已提交
1244
				pattern: typeof selector.pattern === 'undefined' ? undefined : GlobPattern.from(selector.pattern),
1245 1246 1247
				exclusive: selector.exclusive
			};
		}
1248
	}
1249
}
1250 1251

export namespace LogLevel {
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
	export function from(extLevel: types.LogLevel): _MainLogLevel {
		switch (extLevel) {
			case types.LogLevel.Trace:
				return _MainLogLevel.Trace;
			case types.LogLevel.Debug:
				return _MainLogLevel.Debug;
			case types.LogLevel.Info:
				return _MainLogLevel.Info;
			case types.LogLevel.Warning:
				return _MainLogLevel.Warning;
			case types.LogLevel.Error:
				return _MainLogLevel.Error;
			case types.LogLevel.Critical:
				return _MainLogLevel.Critical;
			case types.LogLevel.Off:
				return _MainLogLevel.Off;
M
mtaran-google 已提交
1268 1269
			default:
				return _MainLogLevel.Info;
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
		}
	}

	export function to(mainLevel: _MainLogLevel): types.LogLevel {
		switch (mainLevel) {
			case _MainLogLevel.Trace:
				return types.LogLevel.Trace;
			case _MainLogLevel.Debug:
				return types.LogLevel.Debug;
			case _MainLogLevel.Info:
				return types.LogLevel.Info;
			case _MainLogLevel.Warning:
				return types.LogLevel.Warning;
			case _MainLogLevel.Error:
				return types.LogLevel.Error;
			case _MainLogLevel.Critical:
				return types.LogLevel.Critical;
			case _MainLogLevel.Off:
				return types.LogLevel.Off;
M
mtaran-google 已提交
1289 1290
			default:
				return types.LogLevel.Info;
1291
		}
1292 1293
	}
}