如何在 Visual Studio 2017 中更改 Ctrl+C 以复制 Word,而不是整行 [英] How to Change Ctrl+C in Visual Studio 2017 to Copy Word, Not Entire Line

查看:32
本文介绍了如何在 Visual Studio 2017 中更改 Ctrl+C 以复制 Word,而不是整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似于在 Visual Studio 中禁用单行复制,除了我想将其更改为仅复制光标所在的单词,如果未选择任何内容.如果它在空白区域,当然,我不在乎,复制该行,但 99% 我正在尝试复制一个单词,而不是该行这可能吗?

This question is similar to Disabling single line copy in Visual Studio, except that I'm wanting to change it to just copy the word the cursor is on, if nothing is not selected. If it's on white space, sure, I don't care, copy the line, but 99% I'm trying to copy a word, not the line Is this possible?

推荐答案

要复制插入符号所在的单词,您可以为以下内容指定快捷方式 Visual Commander(由我开发)命令(C#语言):

To copy the word the caret is on, you can assign a shortcut to the following Visual Commander (developed by me) command (language C#):

public class C : VisualCommanderExt.ICommand
{
    public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) 
    {
        this.DTE = DTE;
        EnvDTE.TextSelection ts = TryGetFocusedDocumentSelection();
        if (ts != null && ts.IsEmpty)
            CopyWord(ts);
        else if (IsCommandAvailable("Edit.Copy"))
            DTE.ExecuteCommand("Edit.Copy");
    }

    private void CopyWord(EnvDTE.TextSelection ts)
    {
        EnvDTE.EditPoint left = ts.ActivePoint.CreateEditPoint();
        left.WordLeft();

        EnvDTE.EditPoint right = ts.ActivePoint.CreateEditPoint();
        right.WordRight();

        System.Windows.Clipboard.SetText(left.GetText(right));
    }

    private EnvDTE.TextSelection TryGetFocusedDocumentSelection()
    {
        try
        {
            return DTE.ActiveWindow.Document.Selection as EnvDTE.TextSelection;
        }
        catch(System.Exception)
        {
        }
        return null;
    }

    private bool IsCommandAvailable(string commandName)
    {
        EnvDTE80.Commands2 commands = DTE.Commands as EnvDTE80.Commands2;
        if (commands == null)
            return false;
        EnvDTE.Command command = commands.Item(commandName, 0);
        if (command == null)
            return false;
        return command.IsAvailable;
    }

    private EnvDTE80.DTE2 DTE;
}

这篇关于如何在 Visual Studio 2017 中更改 Ctrl+C 以复制 Word,而不是整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆