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

import Severity from 'vs/base/common/severity';
8
import * as modes from 'vs/editor/common/modes';
9
import * as types from './extHostTypes';
10
import { Position as EditorPosition } from 'vs/platform/editor/common/editor';
A
Alex Dima 已提交
11
import { IDecorationOptions, EndOfLineSequence } from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
12
import * as vscode from 'vscode';
13
import URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
14
import { ProgressLocation as MainProgressLocation } from 'vs/platform/progress/common/progress';
15
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
16 17 18
import { IPosition } from 'vs/editor/common/core/position';
import { IRange } from 'vs/editor/common/core/range';
import { ISelection } from 'vs/editor/common/core/selection';
E
Erich Gamma 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

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

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

export interface SelectionLike extends RangeLike {
	anchor: PositionLike;
	active: PositionLike;
}

35
export function toSelection(selection: ISelection): types.Selection {
36
	let { selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn } = selection;
37 38 39
	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 已提交
40 41 42
}

export function fromSelection(selection: SelectionLike): ISelection {
43
	let { anchor, active } = selection;
E
Erich Gamma 已提交
44 45 46 47 48 49 50 51 52
	return {
		selectionStartLineNumber: anchor.line + 1,
		selectionStartColumn: anchor.character + 1,
		positionLineNumber: active.line + 1,
		positionColumn: active.character + 1
	};
}

export function fromRange(range: RangeLike): IRange {
J
Johannes Rieken 已提交
53 54 55
	if (!range) {
		return undefined;
	}
56
	let { start, end } = range;
E
Erich Gamma 已提交
57 58 59 60 61 62 63 64
	return {
		startLineNumber: start.line + 1,
		startColumn: start.character + 1,
		endLineNumber: end.line + 1,
		endColumn: end.character + 1
	};
}

65
export function toRange(range: IRange): types.Range {
J
Johannes Rieken 已提交
66 67 68
	if (!range) {
		return undefined;
	}
69
	let { startLineNumber, startColumn, endLineNumber, endColumn } = range;
70
	return new types.Range(startLineNumber - 1, startColumn - 1, endLineNumber - 1, endColumn - 1);
E
Erich Gamma 已提交
71 72
}

73 74
export function toPosition(position: IPosition): types.Position {
	return new types.Position(position.lineNumber - 1, position.column - 1);
E
Erich Gamma 已提交
75 76
}

77 78
export function fromPosition(position: types.Position): IPosition {
	return { lineNumber: position.line + 1, column: position.character + 1 };
79 80
}

E
Erich Gamma 已提交
81 82
export function fromDiagnosticSeverity(value: number): Severity {
	switch (value) {
83
		case types.DiagnosticSeverity.Error:
E
Erich Gamma 已提交
84
			return Severity.Error;
85
		case types.DiagnosticSeverity.Warning:
E
Erich Gamma 已提交
86
			return Severity.Warning;
87
		case types.DiagnosticSeverity.Information:
E
Erich Gamma 已提交
88
			return Severity.Info;
89
		case types.DiagnosticSeverity.Hint:
E
Erich Gamma 已提交
90 91 92 93 94
			return Severity.Ignore;
	}
	return Severity.Error;
}

95
export function toDiagnosticSeverty(value: Severity): types.DiagnosticSeverity {
E
Erich Gamma 已提交
96 97
	switch (value) {
		case Severity.Info:
98
			return types.DiagnosticSeverity.Information;
E
Erich Gamma 已提交
99
		case Severity.Warning:
100
			return types.DiagnosticSeverity.Warning;
E
Erich Gamma 已提交
101
		case Severity.Error:
102
			return types.DiagnosticSeverity.Error;
E
Erich Gamma 已提交
103
		case Severity.Ignore:
104
			return types.DiagnosticSeverity.Hint;
E
Erich Gamma 已提交
105
	}
106
	return types.DiagnosticSeverity.Error;
E
Erich Gamma 已提交
107 108 109
}

export function fromViewColumn(column?: vscode.ViewColumn): EditorPosition {
B
Benjamin Pasero 已提交
110
	let editorColumn = EditorPosition.ONE;
E
Erich Gamma 已提交
111
	if (typeof column !== 'number') {
B
Benjamin Pasero 已提交
112
		// stick with ONE
113
	} else if (column === <number>types.ViewColumn.Two) {
B
Benjamin Pasero 已提交
114
		editorColumn = EditorPosition.TWO;
115
	} else if (column === <number>types.ViewColumn.Three) {
B
Benjamin Pasero 已提交
116
		editorColumn = EditorPosition.THREE;
E
Erich Gamma 已提交
117 118 119 120
	}
	return editorColumn;
}

121 122
export function toViewColumn(position?: EditorPosition): vscode.ViewColumn {
	if (typeof position !== 'number') {
M
Matt Bierner 已提交
123
		return undefined;
124
	}
B
Benjamin Pasero 已提交
125
	if (position === EditorPosition.ONE) {
126
		return <number>types.ViewColumn.One;
B
Benjamin Pasero 已提交
127
	} else if (position === EditorPosition.TWO) {
128
		return <number>types.ViewColumn.Two;
B
Benjamin Pasero 已提交
129
	} else if (position === EditorPosition.THREE) {
130
		return <number>types.ViewColumn.Three;
131
	}
M
Matt Bierner 已提交
132
	return undefined;
133
}
E
Erich Gamma 已提交
134

M
Martin Aeschlimann 已提交
135
function isDecorationOptions(something: any): something is vscode.DecorationOptions {
136
	return (typeof something.range !== 'undefined');
E
Erich Gamma 已提交
137 138
}

139
function isDecorationOptionsArr(something: vscode.Range[] | vscode.DecorationOptions[]): something is vscode.DecorationOptions[] {
E
Erich Gamma 已提交
140 141 142
	if (something.length === 0) {
		return true;
	}
M
Martin Aeschlimann 已提交
143
	return isDecorationOptions(something[0]) ? true : false;
E
Erich Gamma 已提交
144 145
}

146
export function fromRangeOrRangeWithMessage(ranges: vscode.Range[] | vscode.DecorationOptions[]): IDecorationOptions[] {
M
Martin Aeschlimann 已提交
147 148
	if (isDecorationOptionsArr(ranges)) {
		return ranges.map((r): IDecorationOptions => {
E
Erich Gamma 已提交
149 150
			return {
				range: fromRange(r.range),
151
				hoverMessage: r.hoverMessage,
152
				renderOptions: <any> /* URI vs Uri */r.renderOptions
E
Erich Gamma 已提交
153 154 155
			};
		});
	} else {
M
Martin Aeschlimann 已提交
156
		return ranges.map((r): IDecorationOptions => {
E
Erich Gamma 已提交
157 158
			return {
				range: fromRange(r)
B
Benjamin Pasero 已提交
159
			};
E
Erich Gamma 已提交
160 161 162
		});
	}
}
163

164
export const TextEdit = {
165

J
Johannes Rieken 已提交
166 167
	from(edit: vscode.TextEdit): modes.TextEdit {
		return <modes.TextEdit>{
168
			text: edit.newText,
J
Johannes Rieken 已提交
169
			eol: EndOfLine.from(edit.newEol),
170
			range: fromRange(edit.range)
B
Benjamin Pasero 已提交
171
		};
172
	},
J
Johannes Rieken 已提交
173 174 175 176
	to(edit: modes.TextEdit): vscode.TextEdit {
		let result = new types.TextEdit(toRange(edit.range), edit.text);
		result.newEol = EndOfLine.to(edit.eol);
		return result;
177
	}
B
Benjamin Pasero 已提交
178
};
179

180

181 182 183
export namespace SymbolKind {

	const _fromMapping: { [kind: number]: modes.SymbolKind } = Object.create(null);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	_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;
207 208
	_fromMapping[types.SymbolKind.Event] = modes.SymbolKind.Event;
	_fromMapping[types.SymbolKind.Operator] = modes.SymbolKind.Operator;
209
	_fromMapping[types.SymbolKind.TypeParameter] = modes.SymbolKind.TypeParameter;
210 211

	export function from(kind: vscode.SymbolKind): modes.SymbolKind {
212
		return _fromMapping[kind] || modes.SymbolKind.Property;
213 214
	}

215 216 217 218
	export function to(kind: modes.SymbolKind): vscode.SymbolKind {
		for (let k in _fromMapping) {
			if (_fromMapping[k] === kind) {
				return Number(k);
219
			}
220 221
		}
		return types.SymbolKind.Property;
222 223 224
	}
}

225 226
export function fromSymbolInformation(info: vscode.SymbolInformation): modes.SymbolInformation {
	return <modes.SymbolInformation>{
227
		name: info.name,
228
		kind: SymbolKind.from(info.kind),
229
		containerName: info.containerName,
230
		location: location.from(info.location)
231 232 233
	};
}

234 235 236 237
export function toSymbolInformation(bearing: modes.SymbolInformation): types.SymbolInformation {
	return new types.SymbolInformation(
		bearing.name,
		SymbolKind.to(bearing.kind),
238
		bearing.containerName,
239
		location.to(bearing.location)
240
	);
241 242 243
}


244
export const location = {
J
Johannes Rieken 已提交
245
	from(value: vscode.Location): modes.Location {
246
		return {
J
Johannes Rieken 已提交
247 248
			range: value.range && fromRange(value.range),
			uri: <URI>value.uri
J
Johannes Rieken 已提交
249
		};
250
	},
251 252
	to(value: modes.Location): types.Location {
		return new types.Location(value.uri, toRange(value.range));
253
	}
J
Johannes Rieken 已提交
254
};
255

256 257
export function fromHover(hover: vscode.Hover): modes.Hover {
	return <modes.Hover>{
258
		range: fromRange(hover.range),
259
		contents: hover.contents
B
Benjamin Pasero 已提交
260
	};
261 262
}

263
export function toHover(info: modes.Hover): types.Hover {
264
	return new types.Hover(info.contents, toRange(info.range));
265 266
}

267 268
export function toDocumentHighlight(occurrence: modes.DocumentHighlight): types.DocumentHighlight {
	return new types.DocumentHighlight(toRange(occurrence.range), occurrence.kind);
269 270
}

J
Johannes Rieken 已提交
271 272 273 274
export const CompletionItemKind = {

	from(kind: types.CompletionItemKind): modes.SuggestionType {
		switch (kind) {
275
			case types.CompletionItemKind.Method: return 'method';
J
Johannes Rieken 已提交
276 277 278 279 280 281
			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';
282
			case types.CompletionItemKind.Struct: return 'struct';
J
Johannes Rieken 已提交
283 284 285 286
			case types.CompletionItemKind.Module: return 'module';
			case types.CompletionItemKind.Property: return 'property';
			case types.CompletionItemKind.Unit: return 'unit';
			case types.CompletionItemKind.Value: return 'value';
287
			case types.CompletionItemKind.Constant: return 'constant';
J
Johannes Rieken 已提交
288
			case types.CompletionItemKind.Enum: return 'enum';
289
			case types.CompletionItemKind.EnumMember: return 'enum-member';
J
Johannes Rieken 已提交
290 291 292 293 294 295
			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';
296
			case types.CompletionItemKind.Folder: return 'folder';
297 298
			case types.CompletionItemKind.Event: return 'event';
			case types.CompletionItemKind.Operator: return 'operator';
299
			case types.CompletionItemKind.TypeParameter: return 'type-parameter';
J
Johannes Rieken 已提交
300
		}
301
		return 'property';
J
Johannes Rieken 已提交
302 303 304 305
	},

	to(type: modes.SuggestionType): types.CompletionItemKind {
		if (!type) {
306
			return types.CompletionItemKind.Property;
J
Johannes Rieken 已提交
307 308 309 310 311 312
		} else {
			return types.CompletionItemKind[type.charAt(0).toUpperCase() + type.substr(1)];
		}
	}
};

313
export namespace Suggest {
314

315
	export function to(position: types.Position, suggestion: modes.ISuggestion): types.CompletionItem {
316
		const result = new types.CompletionItem(suggestion.label);
317
		result.insertText = suggestion.insertText;
J
Johannes Rieken 已提交
318
		result.kind = CompletionItemKind.to(suggestion.type);
319 320
		result.detail = suggestion.detail;
		result.documentation = suggestion.documentation;
321 322
		result.sortText = suggestion.sortText;
		result.filterText = suggestion.filterText;
323

324
		// 'overwrite[Before|After]'-logic
325
		let overwriteBefore = (typeof suggestion.overwriteBefore === 'number') ? suggestion.overwriteBefore : 0;
M
Martin Aeschlimann 已提交
326 327 328
		let startPosition = new types.Position(position.line, Math.max(0, position.character - overwriteBefore));
		let endPosition = position;
		if (typeof suggestion.overwriteAfter === 'number') {
329 330
			endPosition = new types.Position(position.line, position.character + suggestion.overwriteAfter);
		}
331 332 333 334 335 336 337 338 339 340 341
		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
342

343 344
		return result;
	}
B
Benjamin Pasero 已提交
345
};
346

347 348
export namespace SignatureHelp {

349 350
	export function from(signatureHelp: types.SignatureHelp): modes.SignatureHelp {
		return signatureHelp;
351 352
	}

353 354
	export function to(hints: modes.SignatureHelp): types.SignatureHelp {
		return hints;
355
	}
J
Johannes Rieken 已提交
356 357
}

J
Johannes Rieken 已提交
358 359
export namespace DocumentLink {

360
	export function from(link: vscode.DocumentLink): modes.ILink {
J
Johannes Rieken 已提交
361 362
		return {
			range: fromRange(link.range),
363
			url: link.target && link.target.toString()
J
Johannes Rieken 已提交
364 365 366
		};
	}

367
	export function to(link: modes.ILink): vscode.DocumentLink {
368
		return new types.DocumentLink(toRange(link.range), link.url && URI.parse(link.url));
J
Johannes Rieken 已提交
369 370
	}
}
371

372 373 374 375 376
export namespace TextDocumentSaveReason {

	export function to(reason: SaveReason): vscode.TextDocumentSaveReason {
		switch (reason) {
			case SaveReason.AUTO:
377
				return types.TextDocumentSaveReason.AfterDelay;
378
			case SaveReason.EXPLICIT:
379
				return types.TextDocumentSaveReason.Manual;
380 381 382 383 384
			case SaveReason.FOCUS_CHANGE:
			case SaveReason.WINDOW_CHANGE:
				return types.TextDocumentSaveReason.FocusOut;
		}
	}
385
}
386 387


388 389 390 391 392 393 394 395 396
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;
397
	}
J
Johannes Rieken 已提交
398 399 400 401 402 403 404 405 406

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

J
Johannes Rieken 已提交
409 410 411
export namespace ProgressLocation {
	export function from(loc: vscode.ProgressLocation): MainProgressLocation {
		switch (loc) {
412
			case types.ProgressLocation.SourceControl: return MainProgressLocation.Scm;
J
Johannes Rieken 已提交
413 414 415 416 417
			case types.ProgressLocation.Window: return MainProgressLocation.Window;
		}
		return undefined;
	}
}