Closed vysker closed 7 years ago
Well, you are also modifying the document yourself by calling activeTextEditor.edit
. Don't do that, simply return those edits from the provide-call.
So, I implemented the DocumentRangeFormatter
and got rid of the activeTextEditor.edit
, like you rightfully suggested. Right now, the only thing I return is:
let textEdits: TextEdit[] = [];
textEdits.push(TextEdit.replace(editRange, fixedContent));
return resolve(textEdits);
The DocumentRangeFormatter
definitely receives the TextEdit
array. I checked that:
And yet, nothing happens. Should I be doing anything with them afterwards? Does the subscription handle the rest?
export function activate(context: ExtensionContext): void {
context.subscriptions.push(
languages.registerDocumentRangeFormattingEditProvider(
{ language: 'php', scheme: 'file' },
new PHPDocumentRangeFormattingEditProvider()
)
);
}
Nevermind, got it working!
For some reason I did:
public provideDocumentRangeFormattingEdits(document: TextDocument, range: Range, options: FormattingOptions, token: CancellationToken): Thenable<TextEdit[]> {
return document.save().then(() => { // <--- I don't know why I did this
return this.formatter.formatDocument(document, range);
}
}
I don't know where I got that document.save()
part from. But after removing that, everything is in working order.
Thank you for your time!
Plugging @jrieken here (sorry), because I'm wondering how to fix this issue I'm having with the extension.
I recently adopted the new DocumentFormattingEditProvider API to register my extension for the
editor.formatOnSave
functionality. However, when a file is formatted using formatOnSave, the document will be marked as dirty after formatting. The only time when saving does work, is when I close the document when it is dirty and it prompts me to "Save, Don't save, or Cancel".It's most likely a matter of returning the proper value, or changing the order of execution in the code, but I can't figure out what.
This is how the code abstractly works:
The crucial bit happens here:
Link to code
Do you have any idea as to what I'm doing wrong here?
Thanks in advance!