Skip to content

Commit

Permalink
Merge pull request #7749 from sdedic/lsp/client-save-fixes
Browse files Browse the repository at this point in the history
Proper registration/usage of LSP client ApplyEdits implementation.
  • Loading branch information
sdedic authored Sep 13, 2024
2 parents 51dea0c + 827cda6 commit 93ccaa5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.project.dependency;
package org.netbeans.modules.project.dependency.impl;

import java.io.IOException;
import java.net.URI;
Expand All @@ -34,7 +34,6 @@
import org.netbeans.api.lsp.ResourceOperation;
import org.netbeans.api.lsp.TextDocumentEdit;
import org.netbeans.api.lsp.WorkspaceEdit;
import org.netbeans.modules.project.dependency.impl.TextDocumentEditProcessor;
import org.netbeans.spi.lsp.ApplyEditsImplementation;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.netbeans.api.lsp.ResourceOperation;
import org.netbeans.api.lsp.TextDocumentEdit;
import org.netbeans.api.lsp.WorkspaceEdit;
import org.netbeans.modules.refactoring.spi.ModificationResult;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.URLMapper;
import org.openide.util.NbBundle;
import org.openide.util.Union2;

/**
* Wraps a LSP modification result (WorkspaceEdit) in ProjectModificationResultImpl into a refactoring API's ModificationResult.
Expand Down Expand Up @@ -94,36 +93,7 @@ public Collection<? extends File> getNewFiles() {
})
@Override
public void commit() throws IOException {
// PENDING: the implementation could attach to undoable edits for each of the documents,
// trying to revert if something goes wrong in the middle.

WorkspaceEdit edit = impl.getWorkspaceEdit();

for (Union2<TextDocumentEdit, ResourceOperation> ch : edit.getDocumentChanges()) {
if (ch.hasSecond()) {
ResourceOperation op = ch.second();
if (op instanceof ResourceOperation.CreateFile) {
ResourceOperation.CreateFile cf = (ResourceOperation.CreateFile)op;
FileObject f = ProjectModificationResultImpl.fromString(cf.getNewFile());
if (f.isValid()) {
throw new IOException(Bundle.ERR_CreatedFileAlreadyExists(f.getPath()));
}
FileObject parent = f.getParent();
while (parent != null && parent.isVirtual()) {
parent = parent.getParent();
}
String relative = FileUtil.getRelativePath(parent, f);
// PENDING: how CreateFile denotes creation of a folder (alone) ??
FileUtil.createData(f, relative);
continue;
}

throw new IllegalStateException("Unknown resource operation");
} else if (ch.hasFirst()) {
TextDocumentEdit e = ch.first();
TextDocumentEditProcessor proc = new TextDocumentEditProcessor(e).setSaveAfterEdit(true);
proc.execute();
}
}
WorkspaceEdit.applyEdits(Collections.singletonList(edit), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
package org.netbeans.modules.nbcode.integration;

import org.netbeans.modules.java.lsp.server.ui.AbstractApplyEditsImplementation;
import org.netbeans.spi.lsp.ApplyEditsImplementation;
import org.openide.util.lookup.ServiceProvider;

/**
*
* @author sdedic
*/
@ServiceProvider(service = AbstractApplyEditsImplementation.class, position = 10000)
@ServiceProvider(service = ApplyEditsImplementation.class, position = 10000)
public class LspApplyEditsImplementation extends AbstractApplyEditsImplementation{

}

0 comments on commit 93ccaa5

Please sign in to comment.