Skip to content
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

Command Line Parameters to Open a File at Specified Line/Col #493

Open
AmyGilhespy opened this issue Oct 22, 2023 · 2 comments
Open

Command Line Parameters to Open a File at Specified Line/Col #493

AmyGilhespy opened this issue Oct 22, 2023 · 2 comments

Comments

@AmyGilhespy
Copy link

Describe the solution you'd like
I want to be able to open a file from the command line and seek to a specified line and column. This is so I can open a specified file at a line/column position when clicking on a file or on an error message in Godot.

Describe alternatives you've considered
I currently use another editor.

Additional context
In Notepad++, for instance, I use this command:
C:/Program Files/Notepad++/notepad++.exe -lcs -n{line} -c{col} {file}

I would like something similar in RoslynPad.

@qinqoushui
Copy link

in MainViewModelBase.cs,you can use nuget install commandlineparser ,recv args from commandline,and do something
eg:

private void OpenDocumentFromCommandLine()
{
    string[] args = Environment.GetCommandLineArgs();
    if (args.Length > 2)
    {
        OpenDocumentFromCommandLineEx(args);
    }
    else

    if (args.Length > 1)
    {
        string filePath = args[1];

        if (File.Exists(filePath))
        {
            var document = DocumentViewModel.FromPath(filePath);
            OpenDocument(document);
        }
    }

}

 void OpenDocumentFromCommandLineEx(string[] args)
 {
     if (args.Length < 3)
         return;

     //第一个参数必须是路径
     string filePath = args[1];

     if (File.Exists(filePath))
     {
         var document = DocumentViewModel.FromPath(filePath);
         //解析参数
         OpenDocument(document, args); // runAtNet462
     }
 }

 public void OpenDocument(DocumentViewModel document, string[] args)
 {
     OpenDocument(document); //调用原打开文档的方法
     var openDocument = CurrentOpenDocument!;
     Parser.Default.ParseArguments<ExecOptions>(args).WithParsed(op =>
     {
         ExecutionPlatform platform2 = openDocument.AvailablePlatforms.First();
         if (!string.IsNullOrEmpty(op.Platform))
         {
             Settings.DefaultPlatformName = openDocument.AvailablePlatforms.First(r => r.Description.Contains(op.Platform, StringComparison.OrdinalIgnoreCase)).Description;
         }
         Func<Task> task = async () =>
         {
             if (op.AutoRun)
             {
                 await openDocument!.Run(platform2!).ConfigureAwait(true);
                 //执行完才退出
                 if (op.AutoClose)
                 {
                     //程序退出
                     MainWindowClose?.Invoke();
                 }
             }
         };
         openDocument.AutoRun = () => task();
     });
 }

 class ExecOptions
 {
     [Option('p', "platform", Required = false, HelpText = "运行的目标框架")]
     public string? Platform { get; set; }

     [Option('r', "run", Required = false, HelpText = "是否在打开后立即自动执行")]
     public bool AutoRun { get; set; }

     [Option('c', "close", Required = false, HelpText = "是否在执行完成后自动关闭并退出")]
     public bool AutoClose { get; set; }
 }

@qinqoushui
Copy link

scroll at DocumentView.cs, eg

public void SelectText(int start, int end)
{
    Editor.ScrollTo(start, end);
    Editor.SelectionLength = 1;
    Editor.Focus();

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants