Skip to content

Commit

Permalink
Fix non-sticky outdent action for OnEnter rules (#2619)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Sigwart <ssigwart@gmail.com>
  • Loading branch information
lionel- and ssigwart authored Apr 5, 2024
1 parent 7d480eb commit ee1bb13
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
4 changes: 1 addition & 3 deletions extensions/positron-r/src/test/snapshots/indentation-cases.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ data |>

# ---
# Continuing a one-liner pipeline (after a comment line)
# FIXME (once we merge the sticky dedent PR)
# FIXME
data |>
fn1() |>
# foo
Expand Down Expand Up @@ -113,14 +113,12 @@ data |>
# ---
# Stickiness of dedent after pipeline
# https://github.com/posit-dev/positron/issues/1727
# FIXME
data |>
fn()
"<>"

# ---
# Stickiness of dedent after pipeline (trailing comment)
# FIXME
data |>
fn()
"<>" # foo
Expand Down
10 changes: 4 additions & 6 deletions extensions/positron-r/src/test/snapshots/indentation-snapshots.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ data |>

# ---
# Continuing a one-liner pipeline (after a comment line)
# FIXME (once we merge the sticky dedent PR)
# FIXME
data |>
fn1() |>
# foo
Expand All @@ -108,7 +108,7 @@ data |>
fn1() |>
# foo

"<>"
"<>"

# ---
# Continuing a one-liner pipeline (longer pipeline)
Expand Down Expand Up @@ -198,7 +198,6 @@ data |>
# ---
# Stickiness of dedent after pipeline
# https://github.com/posit-dev/positron/issues/1727
# FIXME
data |>
fn()
"<>"
Expand All @@ -207,11 +206,10 @@ data |>
data |>
fn()

"<>"
"<>"

# ---
# Stickiness of dedent after pipeline (trailing comment)
# FIXME
data |>
fn()
"<>" # foo
Expand All @@ -220,7 +218,7 @@ data |>
data |>
fn()

"<>" # foo
"<>"# foo

# ---
# Indent after function in call
Expand Down
44 changes: 39 additions & 5 deletions src/vs/editor/common/languages/autoIndent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export function getInheritIndentForLine(
model: IVirtualModel,
lineNumber: number,
honorIntentialIndent: boolean = true,
languageConfigurationService: ILanguageConfigurationService
// --- Start Positron ---
languageConfigurationService: ILanguageConfigurationService,
indentConverter: IIndentConverter | undefined = undefined
// --- End Positron ---
): { indentation: string; action: IndentAction | null; line?: number } | null {
if (autoIndent < EditorAutoIndentStrategy.Full) {
return null;
Expand Down Expand Up @@ -165,8 +168,33 @@ export function getInheritIndentForLine(
}

if (honorIntentialIndent) {
// --- Start Positron ---
// Patch from https://github.com/microsoft/vscode/pull/136593
// For https://github.com/posit-dev/positron/issues/1727

let indentation = strings.getLeadingWhitespace(model.getLineContent(precedingUnIgnoredLine));
// Check for onEnter rules that should decrease the indent
if (indentConverter) {
const richEditSupport = languageConfigurationService.getLanguageConfiguration(model.tokenization.getLanguageId());
if (richEditSupport) {
const previousLineText = precedingUnIgnoredLine < 1 ? '' : model.getLineContent(precedingUnIgnoredLine - 1);
const afterEnterText = '';
const enterResult = richEditSupport.onEnter(autoIndent, previousLineText, precedingUnIgnoredLineContent, afterEnterText);
if (enterResult) {
if (enterResult.indentAction === IndentAction.Outdent) {
indentation = indentConverter.unshiftIndent(indentation);
} else if (enterResult.removeText && indentation.length >= enterResult.removeText) {
indentation = indentation.substring(0, indentation.length - enterResult.removeText - 1);
}
}
}
}
// --- End Positron ---

return {
indentation: strings.getLeadingWhitespace(model.getLineContent(precedingUnIgnoredLine)),
// --- Start Positron ---
indentation,
// --- End Positron ---
action: null,
line: precedingUnIgnoredLine
};
Expand Down Expand Up @@ -235,7 +263,9 @@ export function getGoodIndentForLine(
return null;
}

const indent = getInheritIndentForLine(autoIndent, virtualModel, lineNumber, undefined, languageConfigurationService);
// --- Start Positron ---
const indent = getInheritIndentForLine(autoIndent, virtualModel, lineNumber, undefined, languageConfigurationService, indentConverter);
// --- End Positron ---
const lineContent = virtualModel.getLineContent(lineNumber);

if (indent) {
Expand Down Expand Up @@ -361,7 +391,9 @@ export function getIndentForEnter(
};

const currentLineIndent = strings.getLeadingWhitespace(lineTokens.getLineContent());
const afterEnterAction = getInheritIndentForLine(autoIndent, virtualModel, range.startLineNumber + 1, undefined, languageConfigurationService);
// --- Start Positron ---
const afterEnterAction = getInheritIndentForLine(autoIndent, virtualModel, range.startLineNumber + 1, undefined, languageConfigurationService, indentConverter);
// --- End Positron ---
if (!afterEnterAction) {
const beforeEnter = embeddedLanguage ? currentLineIndent : beforeEnterIndent;
return {
Expand Down Expand Up @@ -430,7 +462,9 @@ export function getIndentActionForType(
if (!indentRulesSupport.shouldDecrease(beforeTypeText + afterTypeText) && indentRulesSupport.shouldDecrease(beforeTypeText + ch + afterTypeText)) {
// after typing `ch`, the content matches decreaseIndentPattern, we should adjust the indent to a good manner.
// 1. Get inherited indent action
const r = getInheritIndentForLine(autoIndent, model, range.startLineNumber, false, languageConfigurationService);
// --- Start Positron ---
const r = getInheritIndentForLine(autoIndent, model, range.startLineNumber, false, languageConfigurationService, indentConverter);
// --- End Positron ---
if (!r) {
return null;
}
Expand Down

0 comments on commit ee1bb13

Please sign in to comment.