extHostTypeConverters.ts 33.8 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/parts/search/common/search';
9
import { ITextEditorOptions } from 'vs/platform/editor/common/editor';
10
import { EditorViewColumn } from 'vs/workbench/api/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';
J
Johannes Rieken 已提交
13
import * 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/services/textfile/common/textfiles';
17 18 19
import { IPosition } from 'vs/editor/common/core/position';
import { IRange } from 'vs/editor/common/core/range';
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 { WorkspaceEditDto, ResourceTextEditDto, ResourceFileEditDto } from 'vs/workbench/api/node/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';
25
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/node/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';
E
Erich Gamma 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

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

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

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

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

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

66 67 68 69
	export function from(range: RangeLike): IRange {
		if (!range) {
			return undefined;
		}
M
Matt Bierner 已提交
70
		const { start, end } = range;
71 72 73 74 75 76
		return {
			startLineNumber: start.line + 1,
			startColumn: start.character + 1,
			endLineNumber: end.line + 1,
			endColumn: end.character + 1
		};
J
Johannes Rieken 已提交
77
	}
E
Erich Gamma 已提交
78

79 80 81 82
	export function to(range: IRange): types.Range {
		if (!range) {
			return undefined;
		}
M
Matt Bierner 已提交
83
		const { startLineNumber, startColumn, endLineNumber, endColumn } = range;
84
		return new types.Range(startLineNumber - 1, startColumn - 1, endLineNumber - 1, endColumn - 1);
J
Johannes Rieken 已提交
85
	}
E
Erich Gamma 已提交
86 87
}

88 89 90 91 92 93 94
export namespace Position {
	export function to(position: IPosition): types.Position {
		return new types.Position(position.lineNumber - 1, position.column - 1);
	}
	export function from(position: types.Position): IPosition {
		return { lineNumber: position.line + 1, column: position.character + 1 };
	}
95 96
}

97 98 99 100 101 102 103 104 105 106
export namespace DiagnosticTag {
	export function from(value: vscode.DiagnosticTag): MarkerTag {
		switch (value) {
			case types.DiagnosticTag.Unnecessary:
				return MarkerTag.Unnecessary;
		}
		return undefined;
	}
}

107 108 109 110 111 112
export namespace Diagnostic {
	export function from(value: vscode.Diagnostic): IMarkerData {
		return {
			...Range.from(value.range),
			message: value.message,
			source: value.source,
S
Sandeep Somavarapu 已提交
113
			code: isString(value.code) || isNumber(value.code) ? String(value.code) : void 0,
114
			severity: DiagnosticSeverity.from(value.severity),
115
			relatedInformation: value.relatedInformation && value.relatedInformation.map(DiagnosticRelatedInformation.from),
116
			tags: Array.isArray(value.tags) ? value.tags.map(DiagnosticTag.from) : undefined,
117 118
		};
	}
119 120
}

121 122 123 124 125 126 127 128 129 130
export namespace DiagnosticRelatedInformation {
	export function from(value: types.DiagnosticRelatedInformation): IRelatedInformation {
		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 已提交
131 132
	}
}
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
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 已提交
148

149 150 151 152 153 154 155 156 157 158 159 160
	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;
		}
		return types.DiagnosticSeverity.Error;
E
Erich Gamma 已提交
161 162 163
	}
}

164
export namespace ViewColumn {
165
	export function from(column?: vscode.ViewColumn): EditorViewColumn {
166 167
		if (typeof column === 'number' && column >= types.ViewColumn.One) {
			return column - 1; // adjust zero index (ViewColumn.ONE => 0)
168
		}
169 170 171 172 173 174

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

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

177
	export function to(position?: EditorViewColumn): vscode.ViewColumn {
178 179
		if (typeof position === 'number' && position >= 0) {
			return position + 1; // adjust to index (ViewColumn.ONE => 1)
180
		}
181

M
Matt Bierner 已提交
182
		return undefined;
183 184
	}
}
E
Erich Gamma 已提交
185

M
Martin Aeschlimann 已提交
186
function isDecorationOptions(something: any): something is vscode.DecorationOptions {
187
	return (typeof something.range !== 'undefined');
E
Erich Gamma 已提交
188 189
}

190
export function isDecorationOptionsArr(something: vscode.Range[] | vscode.DecorationOptions[]): something is vscode.DecorationOptions[] {
E
Erich Gamma 已提交
191 192 193
	if (something.length === 0) {
		return true;
	}
M
Martin Aeschlimann 已提交
194
	return isDecorationOptions(something[0]) ? true : false;
E
Erich Gamma 已提交
195 196
}

197 198 199 200 201 202
export namespace MarkdownString {

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

J
Johannes Rieken 已提交
203 204 205 206 207 208
	interface Codeblock {
		language: string;
		value: string;
	}

	function isCodeblock(thing: any): thing is Codeblock {
J
Johannes Rieken 已提交
209
		return thing && typeof thing === 'object'
J
Johannes Rieken 已提交
210 211 212 213
			&& typeof (<Codeblock>thing).language === 'string'
			&& typeof (<Codeblock>thing).value === 'string';
	}

214
	export function from(markup: vscode.MarkdownString | vscode.MarkedString): htmlContent.IMarkdownString {
215
		let res: htmlContent.IMarkdownString;
J
Johannes Rieken 已提交
216 217
		if (isCodeblock(markup)) {
			const { language, value } = markup;
218
			res = { value: '```' + language + '\n' + value + '\n```\n' };
J
Johannes Rieken 已提交
219
		} else if (htmlContent.isMarkdownString(markup)) {
220
			res = markup;
J
Johannes Rieken 已提交
221
		} else if (typeof markup === 'string') {
222
			res = { value: <string>markup };
223
		} else {
224
			res = { value: '' };
225
		}
J
Johannes Rieken 已提交
226

227 228
		// extract uris into a separate object
		res.uris = Object.create(null);
J
Johannes Rieken 已提交
229 230 231
		let renderer = new marked.Renderer();
		renderer.image = renderer.link = (href: string): string => {
			try {
J
Johannes Rieken 已提交
232 233 234
				let uri = URI.parse(href, true);
				uri = uri.with({ query: _uriMassage(uri.query, res.uris) });
				res.uris[href] = uri;
J
Johannes Rieken 已提交
235 236 237 238 239
			} catch (e) {
				// ignore
			}
			return '';
		};
240 241 242
		marked(res.value, { renderer });

		return res;
J
Johannes Rieken 已提交
243 244
	}

J
Johannes Rieken 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	function _uriMassage(part: string, bucket: { [n: string]: UriComponents }): string {
		if (!part) {
			return part;
		}
		let data: any;
		try {
			data = parse(decodeURIComponent(part));
		} catch (e) {
			// ignore
		}
		if (!data) {
			return part;
		}
		data = cloneAndChange(data, value => {
			if (value instanceof URI) {
				let key = `__uri_${Math.random().toString(16).slice(2, 8)}`;
				bucket[key] = value;
				return key;
			} else {
				return undefined;
			}
		});
		return encodeURIComponent(JSON.stringify(data));
	}

270 271 272 273
	export function to(value: htmlContent.IMarkdownString): vscode.MarkdownString {
		const ret = new htmlContent.MarkdownString(value.value);
		ret.isTrusted = value.isTrusted;
		return ret;
274
	}
275 276 277 278 279 280 281

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

284
export function fromRangeOrRangeWithMessage(ranges: vscode.Range[] | vscode.DecorationOptions[]): IDecorationOptions[] {
M
Martin Aeschlimann 已提交
285
	if (isDecorationOptionsArr(ranges)) {
286
		return ranges.map(r => {
E
Erich Gamma 已提交
287
			return {
288
				range: Range.from(r.range),
289
				hoverMessage: Array.isArray(r.hoverMessage) ? MarkdownString.fromMany(r.hoverMessage) : r.hoverMessage && MarkdownString.from(r.hoverMessage),
290
				renderOptions: <any> /* URI vs Uri */r.renderOptions
E
Erich Gamma 已提交
291 292 293
			};
		});
	} else {
M
Martin Aeschlimann 已提交
294
		return ranges.map((r): IDecorationOptions => {
E
Erich Gamma 已提交
295
			return {
296
				range: Range.from(r)
B
Benjamin Pasero 已提交
297
			};
E
Erich Gamma 已提交
298 299 300
		});
	}
}
301

A
Alex Dima 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 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
function pathOrURIToURI(value: string | URI): URI {
	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,
			contentIconPath: pathOrURIToURI(options.contentIconPath),
			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,
			gutterIconPath: pathOrURIToURI(options.gutterIconPath),
			gutterIconSize: options.gutterIconSize,
			overviewRulerColor: <string | types.ThemeColor>options.overviewRulerColor,
			before: ThemableDecorationAttachmentRenderOptions.from(options.before),
			after: ThemableDecorationAttachmentRenderOptions.from(options.after),
		};
	}
}

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,
			rangeBehavior: DecorationRangeBehavior.from(options.rangeBehavior),
			overviewRulerLane: options.overviewRulerLane,
			light: ThemableDecorationRenderOptions.from(options.light),
			dark: ThemableDecorationRenderOptions.from(options.dark),

			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,
			gutterIconPath: pathOrURIToURI(options.gutterIconPath),
			gutterIconSize: options.gutterIconSize,
			overviewRulerColor: <string | types.ThemeColor>options.overviewRulerColor,
			before: ThemableDecorationAttachmentRenderOptions.from(options.before),
			after: ThemableDecorationAttachmentRenderOptions.from(options.after),
		};
	}
}

M
Matt Bierner 已提交
422
export namespace TextEdit {
423

M
Matt Bierner 已提交
424
	export function from(edit: vscode.TextEdit): modes.TextEdit {
J
Johannes Rieken 已提交
425
		return <modes.TextEdit>{
426
			text: edit.newText,
J
Johannes Rieken 已提交
427
			eol: EndOfLine.from(edit.newEol),
428
			range: Range.from(edit.range)
B
Benjamin Pasero 已提交
429
		};
M
Matt Bierner 已提交
430 431 432
	}

	export function to(edit: modes.TextEdit): types.TextEdit {
M
Matt Bierner 已提交
433
		const result = new types.TextEdit(Range.to(edit.range), edit.text);
J
Johannes Rieken 已提交
434 435
		result.newEol = EndOfLine.to(edit.eol);
		return result;
436
	}
M
Matt Bierner 已提交
437
}
438

439
export namespace WorkspaceEdit {
440
	export function from(value: vscode.WorkspaceEdit, documents?: ExtHostDocumentsAndEditors): WorkspaceEditDto {
441
		const result: WorkspaceEditDto = {
442
			edits: []
443
		};
J
Johannes Rieken 已提交
444
		for (const entry of (value as types.WorkspaceEdit)._allEntries()) {
445 446 447
			const [uri, uriOrEdits] = entry;
			if (Array.isArray(uriOrEdits)) {
				// text edits
M
Matt Bierner 已提交
448
				const doc = documents ? documents.getDocument(uri.toString()) : undefined;
449
				result.edits.push(<ResourceTextEditDto>{ resource: uri, modelVersionId: doc && doc.version, edits: uriOrEdits.map(TextEdit.from) });
450 451
			} else {
				// resource edits
452
				result.edits.push(<ResourceFileEditDto>{ oldUri: uri, newUri: uriOrEdits, options: entry[2] });
453 454 455 456 457
			}
		}
		return result;
	}

458
	export function to(value: WorkspaceEditDto) {
459 460
		const result = new types.WorkspaceEdit();
		for (const edit of value.edits) {
461 462 463 464 465
			if (Array.isArray((<ResourceTextEditDto>edit).edits)) {
				result.set(
					URI.revive((<ResourceTextEditDto>edit).resource),
					<types.TextEdit[]>(<ResourceTextEditDto>edit).edits.map(TextEdit.to)
				);
J
Johannes Rieken 已提交
466 467 468
			} else {
				result.renameFile(
					URI.revive((<ResourceFileEditDto>edit).oldUri),
469 470
					URI.revive((<ResourceFileEditDto>edit).newUri),
					(<ResourceFileEditDto>edit).options
J
Johannes Rieken 已提交
471
				);
472
			}
473 474 475
		}
		return result;
	}
476 477
}

478

479 480 481
export namespace SymbolKind {

	const _fromMapping: { [kind: number]: modes.SymbolKind } = Object.create(null);
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
	_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;
505 506
	_fromMapping[types.SymbolKind.Event] = modes.SymbolKind.Event;
	_fromMapping[types.SymbolKind.Operator] = modes.SymbolKind.Operator;
507
	_fromMapping[types.SymbolKind.TypeParameter] = modes.SymbolKind.TypeParameter;
508 509

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

513
	export function to(kind: modes.SymbolKind): vscode.SymbolKind {
M
Matt Bierner 已提交
514
		for (const k in _fromMapping) {
515 516
			if (_fromMapping[k] === kind) {
				return Number(k);
517
			}
518 519
		}
		return types.SymbolKind.Property;
520 521 522
	}
}

523 524 525
export namespace WorkspaceSymbol {
	export function from(info: vscode.SymbolInformation): search.IWorkspaceSymbol {
		return <search.IWorkspaceSymbol>{
526 527 528 529 530 531
			name: info.name,
			kind: SymbolKind.from(info.kind),
			containerName: info.containerName,
			location: location.from(info.location)
		};
	}
532
	export function to(info: search.IWorkspaceSymbol): types.SymbolInformation {
533 534 535 536 537 538 539
		return new types.SymbolInformation(
			info.name,
			SymbolKind.to(info.kind),
			info.containerName,
			location.to(info.location)
		);
	}
540 541
}

542
export namespace DocumentSymbol {
543
	export function from(info: vscode.DocumentSymbol): modes.DocumentSymbol {
M
Matt Bierner 已提交
544
		const result: modes.DocumentSymbol = {
545
			name: info.name,
546
			detail: info.detail,
547 548
			range: Range.from(info.range),
			selectionRange: Range.from(info.selectionRange),
549
			kind: SymbolKind.from(info.kind)
550 551 552 553 554 555
		};
		if (info.children) {
			result.children = info.children.map(from);
		}
		return result;
	}
556
	export function to(info: modes.DocumentSymbol): vscode.DocumentSymbol {
M
Matt Bierner 已提交
557
		const result = new types.DocumentSymbol(
558
			info.name,
559
			info.detail,
J
Johannes Rieken 已提交
560
			SymbolKind.to(info.kind),
561 562
			Range.to(info.range),
			Range.to(info.selectionRange),
563
		);
564
		if (info.children) {
565
			result.children = info.children.map(to) as any;
566 567 568
		}
		return result;
	}
569 570
}

M
Matt Bierner 已提交
571 572
export namespace location {
	export function from(value: vscode.Location): modes.Location {
573
		return {
574
			range: value.range && Range.from(value.range),
J
Johannes Rieken 已提交
575
			uri: value.uri
J
Johannes Rieken 已提交
576
		};
M
Matt Bierner 已提交
577 578 579
	}

	export function to(value: modes.Location): types.Location {
580
		return new types.Location(value.uri, Range.to(value.range));
581
	}
M
Matt Bierner 已提交
582
}
583

M
Matt Bierner 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
export namespace DefinitionLink {
	export function from(value: vscode.Location | vscode.DefinitionLink): modes.DefinitionLink {
		const definitionLink = <vscode.DefinitionLink>value;
		const location = <vscode.Location>value;
		return {
			origin: definitionLink.originSelectionRange
				? Range.from(definitionLink.originSelectionRange)
				: undefined,
			uri: definitionLink.targetUri ? definitionLink.targetUri : location.uri,
			range: Range.from(definitionLink.targetRange ? definitionLink.targetRange : location.range),
			selectionRange: definitionLink.targetSelectionRange
				? Range.from(definitionLink.targetSelectionRange)
				: undefined,
		};
	}
}

601 602 603 604 605 606 607
export namespace Hover {
	export function from(hover: vscode.Hover): modes.Hover {
		return <modes.Hover>{
			range: Range.from(hover.range),
			contents: MarkdownString.fromMany(hover.contents)
		};
	}
608

609 610 611
	export function to(info: modes.Hover): types.Hover {
		return new types.Hover(info.contents.map(MarkdownString.to), Range.to(info.range));
	}
612
}
613 614 615 616 617 618 619 620 621 622
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);
	}
623 624
}

M
Matt Bierner 已提交
625
export namespace CompletionTriggerKind {
626
	export function to(kind: modes.CompletionTriggerKind) {
M
Matt Bierner 已提交
627
		switch (kind) {
628
			case modes.CompletionTriggerKind.TriggerCharacter:
M
Matt Bierner 已提交
629
				return types.CompletionTriggerKind.TriggerCharacter;
630
			case modes.CompletionTriggerKind.TriggerForIncompleteCompletions:
631
				return types.CompletionTriggerKind.TriggerForIncompleteCompletions;
632
			case modes.CompletionTriggerKind.Invoke:
M
Matt Bierner 已提交
633 634 635 636 637 638 639
			default:
				return types.CompletionTriggerKind.Invoke;
		}
	}
}

export namespace CompletionContext {
640
	export function to(context: modes.CompletionContext): types.CompletionContext {
M
Matt Bierner 已提交
641
		return {
642
			triggerKind: CompletionTriggerKind.to(context.triggerKind),
M
Matt Bierner 已提交
643 644 645 646 647
			triggerCharacter: context.triggerCharacter
		};
	}
}

M
Matt Bierner 已提交
648
export namespace CompletionItemKind {
J
Johannes Rieken 已提交
649

M
Matt Bierner 已提交
650
	export function from(kind: types.CompletionItemKind): modes.CompletionItemKind {
J
Johannes Rieken 已提交
651
		switch (kind) {
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
			case types.CompletionItemKind.Method: return modes.CompletionItemKind.Method;
			case types.CompletionItemKind.Function: return modes.CompletionItemKind.Function;
			case types.CompletionItemKind.Constructor: return modes.CompletionItemKind.Constructor;
			case types.CompletionItemKind.Field: return modes.CompletionItemKind.Field;
			case types.CompletionItemKind.Variable: return modes.CompletionItemKind.Variable;
			case types.CompletionItemKind.Class: return modes.CompletionItemKind.Class;
			case types.CompletionItemKind.Interface: return modes.CompletionItemKind.Interface;
			case types.CompletionItemKind.Struct: return modes.CompletionItemKind.Struct;
			case types.CompletionItemKind.Module: return modes.CompletionItemKind.Module;
			case types.CompletionItemKind.Property: return modes.CompletionItemKind.Property;
			case types.CompletionItemKind.Unit: return modes.CompletionItemKind.Unit;
			case types.CompletionItemKind.Value: return modes.CompletionItemKind.Value;
			case types.CompletionItemKind.Constant: return modes.CompletionItemKind.Constant;
			case types.CompletionItemKind.Enum: return modes.CompletionItemKind.Enum;
			case types.CompletionItemKind.EnumMember: return modes.CompletionItemKind.EnumMember;
			case types.CompletionItemKind.Keyword: return modes.CompletionItemKind.Keyword;
			case types.CompletionItemKind.Snippet: return modes.CompletionItemKind.Snippet;
			case types.CompletionItemKind.Text: return modes.CompletionItemKind.Text;
			case types.CompletionItemKind.Color: return modes.CompletionItemKind.Color;
			case types.CompletionItemKind.File: return modes.CompletionItemKind.File;
			case types.CompletionItemKind.Reference: return modes.CompletionItemKind.Reference;
			case types.CompletionItemKind.Folder: return modes.CompletionItemKind.Folder;
			case types.CompletionItemKind.Event: return modes.CompletionItemKind.Event;
			case types.CompletionItemKind.Operator: return modes.CompletionItemKind.Operator;
			case types.CompletionItemKind.TypeParameter: return modes.CompletionItemKind.TypeParameter;
J
Johannes Rieken 已提交
677
		}
678
		return modes.CompletionItemKind.Property;
M
Matt Bierner 已提交
679
	}
J
Johannes Rieken 已提交
680

M
Matt Bierner 已提交
681
	export function to(kind: modes.CompletionItemKind): types.CompletionItemKind {
682
		switch (kind) {
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
			case modes.CompletionItemKind.Method: return types.CompletionItemKind.Method;
			case modes.CompletionItemKind.Function: return types.CompletionItemKind.Function;
			case modes.CompletionItemKind.Constructor: return types.CompletionItemKind.Constructor;
			case modes.CompletionItemKind.Field: return types.CompletionItemKind.Field;
			case modes.CompletionItemKind.Variable: return types.CompletionItemKind.Variable;
			case modes.CompletionItemKind.Class: return types.CompletionItemKind.Class;
			case modes.CompletionItemKind.Interface: return types.CompletionItemKind.Interface;
			case modes.CompletionItemKind.Struct: return types.CompletionItemKind.Struct;
			case modes.CompletionItemKind.Module: return types.CompletionItemKind.Module;
			case modes.CompletionItemKind.Property: return types.CompletionItemKind.Property;
			case modes.CompletionItemKind.Unit: return types.CompletionItemKind.Unit;
			case modes.CompletionItemKind.Value: return types.CompletionItemKind.Value;
			case modes.CompletionItemKind.Constant: return types.CompletionItemKind.Constant;
			case modes.CompletionItemKind.Enum: return types.CompletionItemKind.Enum;
			case modes.CompletionItemKind.EnumMember: return types.CompletionItemKind.EnumMember;
			case modes.CompletionItemKind.Keyword: return types.CompletionItemKind.Keyword;
			case modes.CompletionItemKind.Snippet: return types.CompletionItemKind.Snippet;
			case modes.CompletionItemKind.Text: return types.CompletionItemKind.Text;
			case modes.CompletionItemKind.Color: return types.CompletionItemKind.Color;
			case modes.CompletionItemKind.File: return types.CompletionItemKind.File;
			case modes.CompletionItemKind.Reference: return types.CompletionItemKind.Reference;
			case modes.CompletionItemKind.Folder: return types.CompletionItemKind.Folder;
			case modes.CompletionItemKind.Event: return types.CompletionItemKind.Event;
			case modes.CompletionItemKind.Operator: return types.CompletionItemKind.Operator;
			case modes.CompletionItemKind.TypeParameter: return types.CompletionItemKind.TypeParameter;
J
Johannes Rieken 已提交
708
		}
709
		return types.CompletionItemKind.Property;
J
Johannes Rieken 已提交
710
	}
M
Matt Bierner 已提交
711
}
J
Johannes Rieken 已提交
712

713
export namespace CompletionItem {
714

715
	export function to(suggestion: modes.CompletionItem): types.CompletionItem {
716
		const result = new types.CompletionItem(suggestion.label);
717
		result.insertText = suggestion.insertText;
718
		result.kind = CompletionItemKind.to(suggestion.kind);
719
		result.detail = suggestion.detail;
720
		result.documentation = htmlContent.isMarkdownString(suggestion.documentation) ? MarkdownString.to(suggestion.documentation) : suggestion.documentation;
721 722
		result.sortText = suggestion.sortText;
		result.filterText = suggestion.filterText;
J
Johannes Rieken 已提交
723
		result.preselect = suggestion.preselect;
J
Johannes Rieken 已提交
724
		result.commitCharacters = suggestion.commitCharacters;
725
		result.range = Range.to(suggestion.range);
726
		result.keepWhitespace = Boolean(suggestion.insertTextRules & modes.CompletionItemInsertTextRule.KeepWhitespace);
727
		// 'inserText'-logic
728
		if (suggestion.insertTextRules & modes.CompletionItemInsertTextRule.InsertAsSnippet) {
729 730 731 732 733 734
			result.insertText = new types.SnippetString(suggestion.insertText);
		} else {
			result.insertText = suggestion.insertText;
			result.textEdit = new types.TextEdit(result.range, result.insertText);
		}
		// TODO additionalEdits, command
735

736 737
		return result;
	}
738
}
739

740 741 742 743
export namespace ParameterInformation {
	export function from(info: types.ParameterInformation): modes.ParameterInformation {
		return {
			label: info.label,
744
			documentation: MarkdownString.fromStrict(info.documentation)
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
		};
	}
	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,
760
			documentation: MarkdownString.fromStrict(info.documentation),
761 762 763 764 765 766 767 768 769 770 771 772
			parameters: info.parameters && info.parameters.map(ParameterInformation.from)
		};
	}

	export function to(info: modes.SignatureInformation): types.SignatureInformation {
		return {
			label: info.label,
			documentation: htmlContent.isMarkdownString(info.documentation) ? MarkdownString.to(info.documentation) : info.documentation,
			parameters: info.parameters && info.parameters.map(ParameterInformation.to)
		};
	}
}
773

774 775
export namespace SignatureHelp {

776 777 778 779 780 781
	export function from(help: types.SignatureHelp): modes.SignatureHelp {
		return {
			activeSignature: help.activeSignature,
			activeParameter: help.activeParameter,
			signatures: help.signatures && help.signatures.map(SignatureInformation.from)
		};
782 783
	}

784 785 786 787 788 789
	export function to(help: modes.SignatureHelp): types.SignatureHelp {
		return {
			activeSignature: help.activeSignature,
			activeParameter: help.activeParameter,
			signatures: help.signatures && help.signatures.map(SignatureInformation.to)
		};
790
	}
J
Johannes Rieken 已提交
791 792
}

J
Johannes Rieken 已提交
793 794
export namespace DocumentLink {

795
	export function from(link: vscode.DocumentLink): modes.ILink {
J
Johannes Rieken 已提交
796
		return {
797
			range: Range.from(link.range),
798
			url: link.target && link.target.toString()
J
Johannes Rieken 已提交
799 800 801
		};
	}

802
	export function to(link: modes.ILink): vscode.DocumentLink {
803
		return new types.DocumentLink(Range.to(link.range), link.url && URI.parse(link.url));
J
Johannes Rieken 已提交
804 805
	}
}
806

807
export namespace ColorPresentation {
808
	export function to(colorPresentation: modes.IColorPresentation): types.ColorPresentation {
M
Matt Bierner 已提交
809
		const cp = new types.ColorPresentation(colorPresentation.label);
810 811 812 813 814 815 816
		if (colorPresentation.textEdit) {
			cp.textEdit = TextEdit.to(colorPresentation.textEdit);
		}
		if (colorPresentation.additionalTextEdits) {
			cp.additionalTextEdits = colorPresentation.additionalTextEdits.map(value => TextEdit.to(value));
		}
		return cp;
817 818 819 820 821 822 823 824 825 826 827
	}

	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
		};
	}
}

828 829 830 831 832 833 834 835 836
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];
	}
}

837 838 839 840 841
export namespace TextDocumentSaveReason {

	export function to(reason: SaveReason): vscode.TextDocumentSaveReason {
		switch (reason) {
			case SaveReason.AUTO:
842
				return types.TextDocumentSaveReason.AfterDelay;
843
			case SaveReason.EXPLICIT:
844
				return types.TextDocumentSaveReason.Manual;
845 846 847 848 849
			case SaveReason.FOCUS_CHANGE:
			case SaveReason.WINDOW_CHANGE:
				return types.TextDocumentSaveReason.FocusOut;
		}
	}
850
}
851 852


853 854 855 856 857 858 859 860 861
export namespace EndOfLine {

	export function from(eol: vscode.EndOfLine): EndOfLineSequence {
		if (eol === types.EndOfLine.CRLF) {
			return EndOfLineSequence.CRLF;
		} else if (eol === types.EndOfLine.LF) {
			return EndOfLineSequence.LF;
		}
		return undefined;
862
	}
J
Johannes Rieken 已提交
863 864 865 866 867 868 869 870 871

	export function to(eol: EndOfLineSequence): vscode.EndOfLine {
		if (eol === EndOfLineSequence.CRLF) {
			return types.EndOfLine.CRLF;
		} else if (eol === EndOfLineSequence.LF) {
			return types.EndOfLine.LF;
		}
		return undefined;
	}
872 873
}

J
Johannes Rieken 已提交
874 875 876
export namespace ProgressLocation {
	export function from(loc: vscode.ProgressLocation): MainProgressLocation {
		switch (loc) {
877
			case types.ProgressLocation.SourceControl: return MainProgressLocation.Scm;
J
Johannes Rieken 已提交
878
			case types.ProgressLocation.Window: return MainProgressLocation.Window;
879
			case types.ProgressLocation.Notification: return MainProgressLocation.Notification;
J
Johannes Rieken 已提交
880 881 882 883
		}
		return undefined;
	}
}
884

885
export namespace FoldingRange {
886
	export function from(r: vscode.FoldingRange): modes.FoldingRange {
M
Matt Bierner 已提交
887
		const range: modes.FoldingRange = { start: r.start + 1, end: r.end + 1 };
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
		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;
			}
		}
		return void 0;
908 909 910
	}
}

911
export namespace TextEditorOptions {
912

913 914 915 916 917 918 919 920
	export function from(options?: vscode.TextDocumentShowOptions): ITextEditorOptions {
		if (options) {
			return {
				pinned: typeof options.preview === 'boolean' ? !options.preview : undefined,
				preserveFocus: options.preserveFocus,
				selection: typeof options.selection === 'object' ? Range.from(options.selection) : undefined
			} as ITextEditorOptions;
		}
921

922
		return undefined;
923 924 925 926
	}

}

927
export namespace GlobPattern {
928

929 930 931 932 933
	export function from(pattern: vscode.GlobPattern): string | types.RelativePattern {
		if (pattern instanceof types.RelativePattern) {
			return pattern;
		}

934 935 936
		if (typeof pattern === 'string') {
			return pattern;
		}
937

938 939 940 941 942
		if (isRelativePattern(pattern)) {
			return new types.RelativePattern(pattern.base, pattern.pattern);
		}

		return pattern; // preserve `undefined` and `null`
943 944
	}

945 946 947 948
	function isRelativePattern(obj: any): obj is vscode.RelativePattern {
		const rp = obj as vscode.RelativePattern;
		return rp && typeof rp.base === 'string' && typeof rp.pattern === 'string';
	}
949 950
}

951
export namespace LanguageSelector {
952

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
	export function from(selector: vscode.DocumentSelector): languageSelector.LanguageSelector {
		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,
				pattern: GlobPattern.from(selector.pattern),
				exclusive: selector.exclusive
			};
		}
968
	}
969
}