Skip to content

Conversation

@mkhuzaima
Copy link
Contributor

Fixes #241007

Bug

drag-directory-issue.webm

After Fix

drag-directory-fix.webm

Previously, it was not set for directory
@TedVu
Copy link

TedVu commented Mar 15, 2025

Couldn't reproduce on the latest version.

@mkhuzaima
Copy link
Contributor Author

Couldn't reproduce on the latest version.

Are you using ubuntu ?

@TedVu
Copy link

TedVu commented Mar 16, 2025

I'm using Windows 11, what's the directory structure ? I created a folder and dragged it to an md file, it appeared fine.

@mkhuzaima
Copy link
Contributor Author

@TedVu The issue occurs on Ubuntu but works fine on Windows. When dragging a directory into a Markdown file, it inserts the absolute path instead of the relative path and does not generate a link.

@mjbvz mjbvz added this to the March 2025 milestone Mar 18, 2025
@mjbvz mjbvz enabled auto-merge March 18, 2025 22:32
const { selection, uri } = extractSelection(resourceOrEditor);
editor = { resource: uri, options: selection ? { selection } : undefined };
} else if (!resourceOrEditor.isDirectory) {
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of the code below assumes that resource will be a file, not a directory

Instead I think this check should happen down where we set the Mimes.uriList data. That logic should be updated to also include directories in the url-list

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will update accordingly.

Can you describe some scenarios, through which I can test the change?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should work the same as with the PR but using a more correct data-transfer type instead of the editors type. That could potentially result in VS Code trying to open a folder as an editor, while url-list should work for any type of file or folder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. 👍
Thank you for your prompt reply.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the code in detail and found that the flow is identical for both files and directories. To ensure that the if (draggedEditors.length) condition is met (which is required for setting the MIME type), we need to add the editor to the draggedEditors list.

Additionally, textFileModel is undefined for both files and directories:

const textFileModel = textFileService.files.get(resource);
if (textFileModel) {

The most reasonable approach seems to be adding a separate condition for directories, like this:

} else if (!resourceOrEditor.isDirectory) {
	editor = {
		resource: resourceOrEditor.resource,
		options: { selection: resourceOrEditor.selection }
	};
} else if (resourceOrEditor.isDirectory) {
	editor = {
		resource: resourceOrEditor.resource,
		options: { selection: undefined } // or resourceOrEditor.selection
	};
}

Since this is my first contribution to this repo, I’d appreciate any guidance on whether this approach aligns with the expected changes or if there are additional considerations I should address.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be easier to add a new draggedDirectories variable to go with with draggedEditors. A directory should never end up in draggedEditors

Then update the code where we build the uriListEntries to loop through both the draggedEditors and draggedDirectories

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will update the code soon.

auto-merge was automatically disabled March 20, 2025 22:24

Head branch was pushed to by a user without write access


// Text: allows to paste into text-capable areas
const lineDelimiter = isWindows ? '\r\n' : '\n';
event.dataTransfer.setData(DataTransfers.TEXT, fileSystemResources.map(({ resource }) => labelService.getUriLabel(resource, { noPrefix: true })).join(lineDelimiter));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to write both files and dirs in this case

It looks like maybe a few too many lines got touch in this change. Maybe instead of adding draggedDirectories and draggedFiles, you can make the fix clearer by focusing on the part below where we built the url-list. You can use use fileSystemResources to get the directories and combine those with the dragged editors. Something like:

if (draggedEditors.length) {
   event.dataTransfer.setData(CodeDataTransfers.EDITORS, stringify(draggedEditors));
}

const allDraggedResources = [
    ...draggedEditors.map(editor => resource),
    ...fileSystemResources.filter(({ isDirectory }) => isDirectory).map(dir => dir.resource)
]
if (allDraggedResources.length) {
    // set url-list
}

@mkhuzaima mkhuzaima marked this pull request as draft March 21, 2025 00:27
Copy link
Collaborator

@mjbvz mjbvz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor note but otherwise looks good. Thanks for following up!


const draggedDirectories: URI[] = [
...fileSystemResources.filter(({ isDirectory }) => isDirectory).map(({ resource }) => resource),
];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since filter creates a copy of the array, you can simplify this to: const draggedDirectories = fileSystemResources .filter(...) without using spread

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

const draggedDirectories: URI[] = fileSystemResources.filter(({ isDirectory }) => isDirectory).map(({ resource }) => resource);

if (draggedEditors.length || draggedDirectories.length) {
event.dataTransfer.setData(CodeDataTransfers.EDITORS, stringify([...draggedEditors, ...draggedDirectories]));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't set directories here, it does not solve the bug.

@mkhuzaima mkhuzaima marked this pull request as ready for review March 21, 2025 11:40
@mkhuzaima mkhuzaima requested a review from mjbvz March 21, 2025 11:42
@mkhuzaima
Copy link
Contributor Author

@mjbvz I have updated the PR based on the requested changes. Please review it when you have time.
I appreciate your prompt feedback and guidance.

@mjbvz mjbvz modified the milestones: March 2025, April 2025 Mar 25, 2025
@mjbvz
Copy link
Collaborator

mjbvz commented Mar 25, 2025

@mkhuzaima I think there was a bug with how resources was set. Please test out the change I fixed and let me know if it works for you too

@mjbvz mjbvz enabled auto-merge March 28, 2025 22:53
@mjbvz mjbvz merged commit cc0b0c1 into microsoft:main Mar 28, 2025
7 checks passed
@vs-code-engineering vs-code-engineering bot locked and limited conversation to collaborators May 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown support: issue with dragging folder into editor to create a link on Linux edition of VSCode

4 participants