Skip to content

Commit

Permalink
Fix for action that has two edits on the same line (#161)
Browse files Browse the repository at this point in the history
Signed-off-by: Liam Barry Allan <[email protected]>
  • Loading branch information
worksofliam committed Nov 6, 2022
1 parent 189c97f commit 3fa9c12
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions server/src/providers/linter/documentFormatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { calculateOffset, getActions, getLintOptions } from '.';
import { documents, parser } from '..';
import Linter from '../../language/linter';

export default async function documentFormattingProvider(params: DocumentFormattingParams): Promise<TextEdit[]|undefined> {
export default async function documentFormattingProvider(params: DocumentFormattingParams): Promise<TextEdit[] | undefined> {
const uri = params.textDocument.uri;
const document = documents.get(uri);

Expand All @@ -14,8 +14,8 @@ export default async function documentFormattingProvider(params: DocumentFormatt
const options = await getLintOptions(document.uri);

if (options) {
let docs = await parser.getDocs(document.uri);
let docs = await parser.getDocs(document.uri);

if (docs) {
// Need to fetch the docs again incase comments were added
// as part of RequiresProcedureDescription
Expand All @@ -24,14 +24,14 @@ export default async function documentFormattingProvider(params: DocumentFormatt
});

// Next up, let's fix all the other things!
const {errors} = Linter.getErrors({
const { errors } = Linter.getErrors({
uri: document.uri,
content: document.getText()
}, options, docs);


const actions = getActions(
document,
document,
errors.filter(error => error.type !== `RequiresProcedureDescription`)
);

Expand All @@ -46,16 +46,19 @@ export default async function documentFormattingProvider(params: DocumentFormatt
if (action.edit && action.edit.changes) {
const uris = action.edit.changes;
const suggestedEdits = uris[document.uri];
suggestedEdits.forEach(edit => {
const changedLine = edit.range.start.line;
// We play it safe and don't change the same line twice.
if (linesChanged.includes(changedLine)) {
skippedChanges += 1;
} else {
const editedLineBefore = suggestedEdits[0] ? linesChanged.includes(suggestedEdits[0].range.start.line) : false;
if (!editedLineBefore) {
suggestedEdits.forEach(edit => {
const changedLine = edit.range.start.line;
fixes.push(edit);
linesChanged.push(changedLine);
}
})

if (!linesChanged.includes(changedLine)) {
linesChanged.push(changedLine);
}
});
} else {
skippedChanges += 1;
}
}
});

Expand All @@ -68,7 +71,7 @@ export default async function documentFormattingProvider(params: DocumentFormatt

const indentFixes = indentErrors.map(error => {
const range = calculateOffset(document, {
range: Range.create(error.line, 0, error.line, error.currentIndent),
range: Range.create(error.line, 0, error.line, error.currentIndent),
offset: undefined,
type: undefined,
newValue: undefined,
Expand Down

0 comments on commit 3fa9c12

Please sign in to comment.