giovdk21 / vscode-sublime-merge

Sublime Merge integration for Visual Studio Code
MIT License
15 stars 1 forks source link

Sublime Merge doesn't open repository in nested directory #6

Closed adhamu closed 4 years ago

adhamu commented 4 years ago

My current setup is that I have a top-level code directory where all of my projects (git respositories) live.

Eg.

├── ~/code
│   ├── projectFoo
│       └── .git
│       └── index.js
│   ├── projectBar
│       └── .git
│       └── index.js
│   ├── projectBaz
│       └── .git
│       └── index.js

I add the code folder to my VSCode workspace but upon clicking "File history in Sublime Merge" for example, nothing happens.

It seems that I have to add each project to my VSCode workspace individually so that my structure is like so:

├── projectFoo
    └── .git
    └── index.js
├── projectBar
    └── .git
    └── index.js
├── projectBaz
    └── .git
    └── index.js

Is there any way this extension can detect if it's inside a top-level directory and open the appropriate repository?

giovdk21 commented 4 years ago

Hello, yes this is currently the expected behaviour (also similar to what described in #2)

I'm not sure if that features is supported by the Sublime Text / Sublime Merge integration either, but I had it working this way for two reasons:

Because workspaces seems to be designed to address the exact scenario you describe I'd feel that's a valid solution (I implemented supports for workspaces even though I don't usually use them myself) but please let me know if you have other suggestions.

adhamu commented 4 years ago

I'm not sure. I've been looking at the Predefined Variables and not sure if some of these might be more suitable (I've not looked in detail at what the extension is currently using).

Personally, I use this extension mainly for "open this file in Sublime Merge" and "view line history" if that helps

adhamu commented 4 years ago

FWIW, I had the same directory structure in Sublime Text which seemed to work

adhamu commented 4 years ago

Here's a quick example I've knocked together using Sindre Sorhus's findUp library.

import * as vscode from 'vscode';
import * as child from 'child_process';
import * as findUp from 'find-up';
import * as path from 'path';

const viewFileHistory = async () => {
  if (vscode.window.activeTextEditor) {
    let filePath = vscode.window.activeTextEditor.document.uri.path;
    const findRepo = await findUp('.git', {
      cwd: path.dirname(filePath),
      type: 'directory',
    });

    if (!findRepo) {
      return;
    }

    const repoLocation = path.dirname(findRepo);
    filePath = filePath.replace(`${repoLocation}/`, '');
    child.execFile(`smerge`, ['search', `file:"${filePath}"`], {
      cwd: repoLocation,
    });
  }
};

export function activate(context: vscode.ExtensionContext) {
  const viewFileHistoryCommand = vscode.commands.registerCommand(
    'history-in-sublime-merge.viewFileHistory',
    viewFileHistory
  );

  context.subscriptions.push(viewFileHistoryCommand);
}
giovdk21 commented 4 years ago

Thanks @adhamu

In you're example you are adding the command based on whether a ".git" folder is found as a parent of the current file, however if I remember correctly that subscription is not file specific and set up only once when the extension is activated, right?

Anyway then the main issue would be that I'm using the workspaceContains activation event (https://code.visualstudio.com/api/references/activation-events) to tell vscode when to activate the extension (i.e. only when the workspace contains the .git folder) although I just noticed it should be possible to do something like "workspaceContains:**/.git" (see https://github.com/giovdk21/vscode-sublime-merge/blob/master/package.json#L34)

Anyway I'll give it a go trying what I described above and the info you provided! Thanks.

p.s. can't promise I will be able to try this anytime soon but hopefully at some point in June!

adhamu commented 4 years ago

No worries. I’ll keep an eye out and in the meantime, I’ll have a play with some extension development

adhamu commented 4 years ago

I've had a play and published my own extension for my use-case. I'll close this issue

https://marketplace.visualstudio.com/items?itemName=adhamu.history-in-sublime-merge