Example #1
0
	private static _goodIndentForLine(config: CursorConfiguration, model: ITokenizedModel, lineNumber: number): string {
		let expectedIndentAction = LanguageConfigurationRegistry.getInheritIndentForLine(model, lineNumber, false);

		if (expectedIndentAction) {
			if (expectedIndentAction.action) {
				let indentation = expectedIndentAction.indentation;

				if (expectedIndentAction.action === IndentAction.Indent) {
					indentation = TypeOperations.shiftIndent(config, indentation);
				}

				if (expectedIndentAction.action === IndentAction.Outdent) {
					indentation = TypeOperations.unshiftIndent(config, indentation);
				}

				indentation = config.normalizeIndentation(indentation);

				if (indentation.length === 0) {
					return '';
				} else {
					return indentation;
				}
			}
			else {
				return expectedIndentAction.indentation;
			}
		}

		return null;
	}
	private static _goodIndentForLine(config: CursorConfiguration, model: ITextModel, lineNumber: number): string {
		let action: IndentAction | EnterAction;
		let indentation: string;

		let expectedIndentAction = config.autoIndent ? LanguageConfigurationRegistry.getInheritIndentForLine(model, lineNumber, false) : null;
		if (expectedIndentAction) {
			action = expectedIndentAction.action;
			indentation = expectedIndentAction.indentation;
		} else if (lineNumber > 1) {
			let lastLineNumber = lineNumber - 1;
			for (lastLineNumber = lineNumber - 1; lastLineNumber >= 1; lastLineNumber--) {
				let lineText = model.getLineContent(lastLineNumber);
				let nonWhitespaceIdx = strings.lastNonWhitespaceIndex(lineText);
				if (nonWhitespaceIdx >= 0) {
					break;
				}
			}

			if (lastLineNumber < 1) {
				// No previous line with content found
				return null;
			}

			let maxColumn = model.getLineMaxColumn(lastLineNumber);
			let expectedEnterAction = LanguageConfigurationRegistry.getEnterAction(model, new Range(lastLineNumber, maxColumn, lastLineNumber, maxColumn));
			if (expectedEnterAction) {
				indentation = expectedEnterAction.indentation;
				action = expectedEnterAction.enterAction;
				if (action) {
					indentation += action.appendText;
				}
			}
		}

		if (action) {
			if (action === IndentAction.Indent) {
				indentation = TypeOperations.shiftIndent(config, indentation);
			}

			if (action === IndentAction.Outdent) {
				indentation = TypeOperations.unshiftIndent(config, indentation);
			}

			indentation = config.normalizeIndentation(indentation);
		}

		if (!indentation) {
			return null;
		}

		return indentation;
	}