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

7
import * as modes from 'vs/editor/common/modes';
8
import * as types from './extHostTypes';
9
import * as search from 'vs/workbench/parts/search/common/search';
10
import { ITextEditorOptions } from 'vs/platform/editor/common/editor';
11
import { EditorViewColumn } from 'vs/workbench/api/shared/editor';
12
import { IDecorationOptions } from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
13
import { EndOfLineSequence } from 'vs/editor/common/model';
J
Johannes Rieken 已提交
14
import * as vscode from 'vscode';
15
import URI from 'vs/base/common/uri';
16
import { ProgressLocation as MainProgressLocation } from 'vs/workbench/services/progress/common/progress';
17
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
18 19 20
import { IPosition } from 'vs/editor/common/core/position';
import { IRange } from 'vs/editor/common/core/range';
import { ISelection } from 'vs/editor/common/core/selection';
21
import * as htmlContent from 'vs/base/common/htmlContent';
22
import { IRelativePattern } from 'vs/base/common/glob';
23
import * as languageSelector from 'vs/editor/common/modes/languageSelector';
J
Johannes Rieken 已提交
24
import { WorkspaceEditDto, ResourceTextEditDto, ResourceFileEditDto } from 'vs/workbench/api/node/extHost.protocol';
25
import { MarkerSeverity, IRelatedInformation, IMarkerData, MarkerTag } from 'vs/platform/markers/common/markers';
26
import { ACTIVE_GROUP, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
27
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/node/extHostDocumentsAndEditors';
E
Erich Gamma 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

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

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

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

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

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

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

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

86 87 88 89 90 91 92
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 };
	}
93 94
}

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

105 106 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,
			code: String(value.code),
			severity: DiagnosticSeverity.from(value.severity),
113
			relatedInformation: value.relatedInformation && value.relatedInformation.map(DiagnosticRelatedInformation.from),
114
			tags: Array.isArray(value.tags) ? value.tags.map(DiagnosticTag.from) : undefined,
115 116
		};
	}
117 118
}

119 120 121 122 123 124 125 126 127 128
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 已提交
129 130
	}
}
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
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 已提交
146

147 148 149 150 151 152 153 154 155 156 157 158
	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 已提交
159 160 161
	}
}

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

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

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

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

M
Matt Bierner 已提交
180
		return undefined;
181 182
	}
}
E
Erich Gamma 已提交
183

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

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

195 196 197 198 199 200
export namespace MarkdownString {

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

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

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

212
	export function from(markup: vscode.MarkdownString | vscode.MarkedString): htmlContent.IMarkdownString {
J
Johannes Rieken 已提交
213 214 215 216
		if (isCodeblock(markup)) {
			const { language, value } = markup;
			return { value: '```' + language + '\n' + value + '\n```\n' };
		} else if (htmlContent.isMarkdownString(markup)) {
217
			return markup;
J
Johannes Rieken 已提交
218
		} else if (typeof markup === 'string') {
219
			return { value: <string>markup };
220
		} else {
J
Johannes Rieken 已提交
221
			return { value: '' };
222 223
		}
	}
224 225 226 227
	export function to(value: htmlContent.IMarkdownString): vscode.MarkdownString {
		const ret = new htmlContent.MarkdownString(value.value);
		ret.isTrusted = value.isTrusted;
		return ret;
228
	}
229 230 231 232 233 234 235

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

238
export function fromRangeOrRangeWithMessage(ranges: vscode.Range[] | vscode.DecorationOptions[]): IDecorationOptions[] {
M
Martin Aeschlimann 已提交
239
	if (isDecorationOptionsArr(ranges)) {
240
		return ranges.map(r => {
E
Erich Gamma 已提交
241
			return {
242
				range: Range.from(r.range),
243
				hoverMessage: Array.isArray(r.hoverMessage) ? MarkdownString.fromMany(r.hoverMessage) : r.hoverMessage && MarkdownString.from(r.hoverMessage),
244
				renderOptions: <any> /* URI vs Uri */r.renderOptions
E
Erich Gamma 已提交
245 246 247
			};
		});
	} else {
M
Martin Aeschlimann 已提交
248
		return ranges.map((r): IDecorationOptions => {
E
Erich Gamma 已提交
249
			return {
250
				range: Range.from(r)
B
Benjamin Pasero 已提交
251
			};
E
Erich Gamma 已提交
252 253 254
		});
	}
}
255

256
export const TextEdit = {
257

J
Johannes Rieken 已提交
258 259
	from(edit: vscode.TextEdit): modes.TextEdit {
		return <modes.TextEdit>{
260
			text: edit.newText,
J
Johannes Rieken 已提交
261
			eol: EndOfLine.from(edit.newEol),
262
			range: Range.from(edit.range)
B
Benjamin Pasero 已提交
263
		};
264
	},
265
	to(edit: modes.TextEdit): types.TextEdit {
266
		let result = new types.TextEdit(Range.to(edit.range), edit.text);
J
Johannes Rieken 已提交
267 268
		result.newEol = EndOfLine.to(edit.eol);
		return result;
269
	}
B
Benjamin Pasero 已提交
270
};
271

272
export namespace WorkspaceEdit {
273
	export function from(value: vscode.WorkspaceEdit, documents?: ExtHostDocumentsAndEditors): WorkspaceEditDto {
274
		const result: WorkspaceEditDto = {
275
			edits: []
276
		};
J
Johannes Rieken 已提交
277
		for (const entry of (value as types.WorkspaceEdit)._allEntries()) {
278 279 280
			const [uri, uriOrEdits] = entry;
			if (Array.isArray(uriOrEdits)) {
				// text edits
281
				let doc = documents ? documents.getDocument(uri.toString()) : undefined;
282
				result.edits.push(<ResourceTextEditDto>{ resource: uri, modelVersionId: doc && doc.version, edits: uriOrEdits.map(TextEdit.from) });
283 284
			} else {
				// resource edits
285
				result.edits.push(<ResourceFileEditDto>{ oldUri: uri, newUri: uriOrEdits, options: entry[2] });
286 287 288 289 290
			}
		}
		return result;
	}

291
	export function to(value: WorkspaceEditDto) {
292 293
		const result = new types.WorkspaceEdit();
		for (const edit of value.edits) {
294 295 296 297 298
			if (Array.isArray((<ResourceTextEditDto>edit).edits)) {
				result.set(
					URI.revive((<ResourceTextEditDto>edit).resource),
					<types.TextEdit[]>(<ResourceTextEditDto>edit).edits.map(TextEdit.to)
				);
J
Johannes Rieken 已提交
299 300 301
			} else {
				result.renameFile(
					URI.revive((<ResourceFileEditDto>edit).oldUri),
302 303
					URI.revive((<ResourceFileEditDto>edit).newUri),
					(<ResourceFileEditDto>edit).options
J
Johannes Rieken 已提交
304
				);
305
			}
306 307 308
		}
		return result;
	}
309 310
}

311

312 313 314
export namespace SymbolKind {

	const _fromMapping: { [kind: number]: modes.SymbolKind } = Object.create(null);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	_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;
338 339
	_fromMapping[types.SymbolKind.Event] = modes.SymbolKind.Event;
	_fromMapping[types.SymbolKind.Operator] = modes.SymbolKind.Operator;
340
	_fromMapping[types.SymbolKind.TypeParameter] = modes.SymbolKind.TypeParameter;
341 342

	export function from(kind: vscode.SymbolKind): modes.SymbolKind {
343
		return _fromMapping[kind] || modes.SymbolKind.Property;
344 345
	}

346 347 348 349
	export function to(kind: modes.SymbolKind): vscode.SymbolKind {
		for (let k in _fromMapping) {
			if (_fromMapping[k] === kind) {
				return Number(k);
350
			}
351 352
		}
		return types.SymbolKind.Property;
353 354 355
	}
}

356 357 358
export namespace WorkspaceSymbol {
	export function from(info: vscode.SymbolInformation): search.IWorkspaceSymbol {
		return <search.IWorkspaceSymbol>{
359 360 361 362 363 364
			name: info.name,
			kind: SymbolKind.from(info.kind),
			containerName: info.containerName,
			location: location.from(info.location)
		};
	}
365
	export function to(info: search.IWorkspaceSymbol): types.SymbolInformation {
366 367 368 369 370 371 372
		return new types.SymbolInformation(
			info.name,
			SymbolKind.to(info.kind),
			info.containerName,
			location.to(info.location)
		);
	}
373 374
}

375
export namespace DocumentSymbol {
376
	export function from(info: vscode.DocumentSymbol): modes.DocumentSymbol {
377
		let result: modes.DocumentSymbol = {
378
			name: info.name,
379
			detail: info.detail,
380 381
			range: Range.from(info.range),
			selectionRange: Range.from(info.selectionRange),
382
			kind: SymbolKind.from(info.kind)
383 384 385 386 387 388
		};
		if (info.children) {
			result.children = info.children.map(from);
		}
		return result;
	}
389 390
	export function to(info: modes.DocumentSymbol): vscode.DocumentSymbol {
		let result = new types.DocumentSymbol(
391
			info.name,
392
			info.detail,
J
Johannes Rieken 已提交
393
			SymbolKind.to(info.kind),
394 395
			Range.to(info.range),
			Range.to(info.selectionRange),
396
		);
397
		if (info.children) {
398
			result.children = info.children.map(to) as any;
399 400 401
		}
		return result;
	}
402 403
}

404
export const location = {
J
Johannes Rieken 已提交
405
	from(value: vscode.Location): modes.Location {
406
		return {
407
			range: value.range && Range.from(value.range),
J
Johannes Rieken 已提交
408
			uri: value.uri
J
Johannes Rieken 已提交
409
		};
410
	},
411
	to(value: modes.Location): types.Location {
412
		return new types.Location(value.uri, Range.to(value.range));
413
	}
J
Johannes Rieken 已提交
414
};
415

416 417 418 419 420 421 422
export namespace Hover {
	export function from(hover: vscode.Hover): modes.Hover {
		return <modes.Hover>{
			range: Range.from(hover.range),
			contents: MarkdownString.fromMany(hover.contents)
		};
	}
423

424 425 426
	export function to(info: modes.Hover): types.Hover {
		return new types.Hover(info.contents.map(MarkdownString.to), Range.to(info.range));
	}
427
}
428 429 430 431 432 433 434 435 436 437
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);
	}
438 439
}

M
Matt Bierner 已提交
440 441 442 443 444
export namespace CompletionTriggerKind {
	export function from(kind: modes.SuggestTriggerKind) {
		switch (kind) {
			case modes.SuggestTriggerKind.TriggerCharacter:
				return types.CompletionTriggerKind.TriggerCharacter;
445 446
			case modes.SuggestTriggerKind.TriggerForIncompleteCompletions:
				return types.CompletionTriggerKind.TriggerForIncompleteCompletions;
M
Matt Bierner 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
			case modes.SuggestTriggerKind.Invoke:
			default:
				return types.CompletionTriggerKind.Invoke;
		}
	}
}

export namespace CompletionContext {
	export function from(context: modes.SuggestContext): types.CompletionContext {
		return {
			triggerKind: CompletionTriggerKind.from(context.triggerKind),
			triggerCharacter: context.triggerCharacter
		};
	}
}

J
Johannes Rieken 已提交
463 464 465 466
export const CompletionItemKind = {

	from(kind: types.CompletionItemKind): modes.SuggestionType {
		switch (kind) {
467
			case types.CompletionItemKind.Method: return 'method';
J
Johannes Rieken 已提交
468 469 470 471 472 473
			case types.CompletionItemKind.Function: return 'function';
			case types.CompletionItemKind.Constructor: return 'constructor';
			case types.CompletionItemKind.Field: return 'field';
			case types.CompletionItemKind.Variable: return 'variable';
			case types.CompletionItemKind.Class: return 'class';
			case types.CompletionItemKind.Interface: return 'interface';
474
			case types.CompletionItemKind.Struct: return 'struct';
J
Johannes Rieken 已提交
475 476 477 478
			case types.CompletionItemKind.Module: return 'module';
			case types.CompletionItemKind.Property: return 'property';
			case types.CompletionItemKind.Unit: return 'unit';
			case types.CompletionItemKind.Value: return 'value';
479
			case types.CompletionItemKind.Constant: return 'constant';
J
Johannes Rieken 已提交
480
			case types.CompletionItemKind.Enum: return 'enum';
481
			case types.CompletionItemKind.EnumMember: return 'enum-member';
J
Johannes Rieken 已提交
482 483 484 485 486 487
			case types.CompletionItemKind.Keyword: return 'keyword';
			case types.CompletionItemKind.Snippet: return 'snippet';
			case types.CompletionItemKind.Text: return 'text';
			case types.CompletionItemKind.Color: return 'color';
			case types.CompletionItemKind.File: return 'file';
			case types.CompletionItemKind.Reference: return 'reference';
488
			case types.CompletionItemKind.Folder: return 'folder';
489 490
			case types.CompletionItemKind.Event: return 'event';
			case types.CompletionItemKind.Operator: return 'operator';
491
			case types.CompletionItemKind.TypeParameter: return 'type-parameter';
J
Johannes Rieken 已提交
492
		}
493
		return 'property';
J
Johannes Rieken 已提交
494 495 496 497
	},

	to(type: modes.SuggestionType): types.CompletionItemKind {
		if (!type) {
498
			return types.CompletionItemKind.Property;
J
Johannes Rieken 已提交
499 500 501 502 503 504
		} else {
			return types.CompletionItemKind[type.charAt(0).toUpperCase() + type.substr(1)];
		}
	}
};

505
export namespace Suggest {
506

507
	export function to(position: types.Position, suggestion: modes.ISuggestion): types.CompletionItem {
508
		const result = new types.CompletionItem(suggestion.label);
509
		result.insertText = suggestion.insertText;
J
Johannes Rieken 已提交
510
		result.kind = CompletionItemKind.to(suggestion.type);
511
		result.detail = suggestion.detail;
512
		result.documentation = htmlContent.isMarkdownString(suggestion.documentation) ? MarkdownString.to(suggestion.documentation) : suggestion.documentation;
513 514
		result.sortText = suggestion.sortText;
		result.filterText = suggestion.filterText;
515

516
		// 'overwrite[Before|After]'-logic
517
		let overwriteBefore = (typeof suggestion.overwriteBefore === 'number') ? suggestion.overwriteBefore : 0;
M
Martin Aeschlimann 已提交
518 519 520
		let startPosition = new types.Position(position.line, Math.max(0, position.character - overwriteBefore));
		let endPosition = position;
		if (typeof suggestion.overwriteAfter === 'number') {
521 522
			endPosition = new types.Position(position.line, position.character + suggestion.overwriteAfter);
		}
523 524 525 526 527 528 529 530 531 532 533
		result.range = new types.Range(startPosition, endPosition);

		// 'inserText'-logic
		if (suggestion.snippetType === 'textmate') {
			result.insertText = new types.SnippetString(suggestion.insertText);
		} else {
			result.insertText = suggestion.insertText;
			result.textEdit = new types.TextEdit(result.range, result.insertText);
		}

		// TODO additionalEdits, command
534

535 536
		return result;
	}
537
}
538

539 540 541 542
export namespace ParameterInformation {
	export function from(info: types.ParameterInformation): modes.ParameterInformation {
		return {
			label: info.label,
543
			documentation: MarkdownString.fromStrict(info.documentation)
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
		};
	}
	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,
559
			documentation: MarkdownString.fromStrict(info.documentation),
560 561 562 563 564 565 566 567 568 569 570 571
			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)
		};
	}
}
572

573 574
export namespace SignatureHelp {

575 576 577 578 579 580
	export function from(help: types.SignatureHelp): modes.SignatureHelp {
		return {
			activeSignature: help.activeSignature,
			activeParameter: help.activeParameter,
			signatures: help.signatures && help.signatures.map(SignatureInformation.from)
		};
581 582
	}

583 584 585 586 587 588
	export function to(help: modes.SignatureHelp): types.SignatureHelp {
		return {
			activeSignature: help.activeSignature,
			activeParameter: help.activeParameter,
			signatures: help.signatures && help.signatures.map(SignatureInformation.to)
		};
589
	}
J
Johannes Rieken 已提交
590 591
}

J
Johannes Rieken 已提交
592 593
export namespace DocumentLink {

594
	export function from(link: vscode.DocumentLink): modes.ILink {
J
Johannes Rieken 已提交
595
		return {
596
			range: Range.from(link.range),
597
			url: link.target && link.target.toString()
J
Johannes Rieken 已提交
598 599 600
		};
	}

601
	export function to(link: modes.ILink): vscode.DocumentLink {
602
		return new types.DocumentLink(Range.to(link.range), link.url && URI.parse(link.url));
J
Johannes Rieken 已提交
603 604
	}
}
605

606
export namespace ColorPresentation {
607 608 609 610 611 612 613 614 615
	export function to(colorPresentation: modes.IColorPresentation): types.ColorPresentation {
		let cp = new types.ColorPresentation(colorPresentation.label);
		if (colorPresentation.textEdit) {
			cp.textEdit = TextEdit.to(colorPresentation.textEdit);
		}
		if (colorPresentation.additionalTextEdits) {
			cp.additionalTextEdits = colorPresentation.additionalTextEdits.map(value => TextEdit.to(value));
		}
		return cp;
616 617 618 619 620 621 622 623 624 625 626
	}

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

627 628 629 630 631 632 633 634 635
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];
	}
}

636 637 638 639 640
export namespace TextDocumentSaveReason {

	export function to(reason: SaveReason): vscode.TextDocumentSaveReason {
		switch (reason) {
			case SaveReason.AUTO:
641
				return types.TextDocumentSaveReason.AfterDelay;
642
			case SaveReason.EXPLICIT:
643
				return types.TextDocumentSaveReason.Manual;
644 645 646 647 648
			case SaveReason.FOCUS_CHANGE:
			case SaveReason.WINDOW_CHANGE:
				return types.TextDocumentSaveReason.FocusOut;
		}
	}
649
}
650 651


652 653 654 655 656 657 658 659 660
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;
661
	}
J
Johannes Rieken 已提交
662 663 664 665 666 667 668 669 670

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

J
Johannes Rieken 已提交
673 674 675
export namespace ProgressLocation {
	export function from(loc: vscode.ProgressLocation): MainProgressLocation {
		switch (loc) {
676
			case types.ProgressLocation.SourceControl: return MainProgressLocation.Scm;
J
Johannes Rieken 已提交
677
			case types.ProgressLocation.Window: return MainProgressLocation.Window;
678
			case types.ProgressLocation.Notification: return MainProgressLocation.Notification;
J
Johannes Rieken 已提交
679 680 681 682
		}
		return undefined;
	}
}
683

684
export namespace FoldingRange {
685
	export function from(r: vscode.FoldingRange): modes.FoldingRange {
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
		let range: modes.FoldingRange = { start: r.start + 1, end: r.end + 1 };
		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;
707 708 709
	}
}

710
export namespace TextEditorOptions {
711

712 713 714 715 716 717 718 719
	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;
		}
720

721
		return undefined;
722 723 724 725
	}

}

726
export namespace GlobPattern {
727

728 729 730 731
	export function from(pattern: vscode.GlobPattern): string | IRelativePattern {
		if (typeof pattern === 'string') {
			return pattern;
		}
732

733 734 735 736 737
		if (isRelativePattern(pattern)) {
			return new types.RelativePattern(pattern.base, pattern.pattern);
		}

		return pattern; // preserve `undefined` and `null`
738 739
	}

740 741 742 743
	function isRelativePattern(obj: any): obj is vscode.RelativePattern {
		const rp = obj as vscode.RelativePattern;
		return rp && typeof rp.base === 'string' && typeof rp.pattern === 'string';
	}
744 745
}

746
export namespace LanguageSelector {
747

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
	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
			};
		}
763
	}
764
}