-
Notifications
You must be signed in to change notification settings - Fork 51
feat: support ABAP/ADT files in insert_edit_into_file tool #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tobiasmelcher
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
tobiasmelcher:edit_file_tool_improved_abap_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+78
−2
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,11 +15,17 @@ | |
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import org.eclipse.core.filebuffers.FileBuffers; | ||
| import org.eclipse.core.filebuffers.LocationKind; | ||
| import org.eclipse.core.resources.IFile; | ||
| import org.eclipse.core.resources.IResource; | ||
| import org.eclipse.core.resources.semantic.ISemanticFile; | ||
| import org.eclipse.core.runtime.CoreException; | ||
| import org.eclipse.core.runtime.NullProgressMonitor; | ||
| import org.eclipse.core.runtime.QualifiedName; | ||
| import org.eclipse.core.runtime.Status; | ||
| import org.eclipse.lsp4j.FileChangeType; | ||
| import org.eclipse.swt.widgets.Display; | ||
|
|
||
| import com.microsoft.copilot.eclipse.core.CopilotCore; | ||
| import com.microsoft.copilot.eclipse.core.lsp.protocol.InputSchema; | ||
|
|
@@ -197,6 +203,8 @@ private void applyChangesToFile(String changedContent, IFile file) throws CoreEx | |
| if (!validateEdit(file)) { | ||
| throw new IllegalStateException("File validation failed for " + file.getFullPath()); | ||
| } | ||
| verifyTransportRequestForAdtLock(file); | ||
|
|
||
| ByteArrayInputStream inputStream = getInputStream(changedContent, file); | ||
|
|
||
| // Set the file contents | ||
|
|
@@ -207,6 +215,64 @@ private void applyChangesToFile(String changedContent, IFile file) throws CoreEx | |
|
|
||
| // Close the input stream | ||
| inputStream.close(); | ||
|
|
||
| var buffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(file.getFullPath(), LocationKind.IFILE); | ||
| if (buffer != null && buffer.isDirty()) { | ||
| // Some editors (e.g. the ABAP source editor) do not listen for changes to the underlying file and | ||
| // therefore leave a dirty, out-of-date buffer after we have written the new contents to disk. Force | ||
| // the buffer to reload from disk by reverting it, so the open editor reflects the edit we just applied. | ||
| Display.getDefault().asyncExec(() -> { | ||
| try { | ||
| buffer.revert(new NullProgressMonitor()); | ||
| } catch (CoreException e) { | ||
| CopilotCore.LOGGER.error(e); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private static final String ADT_LOCK_RESULT_CLASS = "com.sap.adt.tools.core.internal.locking.AdtLockResult"; | ||
| private static final QualifiedName ADT_LOCK_RESULT_PROPERTY = new QualifiedName("com.sap.adt.tools.filesystem", | ||
| "LockResult"); | ||
|
|
||
| /** | ||
| * When an ADT (ABAP Development Tools) file is locked in a transport-relevant way, it must be associated with a | ||
| * transport request before it can be edited. The lock result is stored as a session property on the semantic file | ||
| * and is accessed reflectively, as the ADT classes are not available at compile time. | ||
| * | ||
| * @param file the file about to be changed | ||
| * @throws CoreException if the file is transport-relevant but has no transport request number assigned | ||
| */ | ||
| private void verifyTransportRequestForAdtLock(IFile file) throws CoreException { | ||
| var semanticFile = file.getAdapter(ISemanticFile.class); | ||
| if (semanticFile == null) { | ||
| return; | ||
| } | ||
| Object lockResult = semanticFile.getSessionProperty(ADT_LOCK_RESULT_PROPERTY); | ||
| if (lockResult == null || !ADT_LOCK_RESULT_CLASS.equals(lockResult.getClass().getCanonicalName())) { | ||
| return; | ||
| } | ||
| Boolean transportRelevant = readField(lockResult, "transportRelevant", Boolean.class); | ||
| if (!Boolean.TRUE.equals(transportRelevant)) { | ||
| return; | ||
| } | ||
| String transportRequestNumber = readField(lockResult, "transportRequestNumber", String.class); | ||
| if (transportRequestNumber == null || transportRequestNumber.isEmpty()) { | ||
| throw new CoreException(Status.error(String.format( | ||
| "Cannot edit %s: the file is transport-relevant but no transport request number is assigned.", | ||
| file.getFullPath()))); | ||
| } | ||
| } | ||
|
|
||
| private <T> T readField(Object target, String fieldName, Class<T> type) { | ||
| try { | ||
| var field = target.getClass().getDeclaredField(fieldName); | ||
| field.setAccessible(true); | ||
| return type.cast(field.get(target)); | ||
| } catch (ReflectiveOperationException | SecurityException e) { | ||
| CopilotCore.LOGGER.error("Failed to read field '" + fieldName + "' from " + target.getClass().getName(), e); | ||
| return null; | ||
| } | ||
| } | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done with [40fe27c] |
||
|
|
||
| private ByteArrayInputStream getInputStream(String changedContent, IFile file) { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done with [40fe27c]