如何在Visual Studio IDE中以从右到左的语言进行注释

17

我想在Visual Studio中用波斯语(一种从右到左的语言)发表评论,就像这样:

//.برای نگهداری مقدار اولیه ی کالا می باشد value_ متغیر 

但似乎代码编辑器不支持RTL语言并重新排列单词,因此结果为:

 //متغیر _value برای نگهداری مقدار اولیه ی کالا می باشد.

这并不是我预期的阅读体验!

有人对这个问题有解决方案吗?


请指定 Visual Studio 版本。 - Ahmad
5个回答

13

如Ahmad所提到的,微软的Visual Studio IDE不支持从右到左的语言(遗憾的是),从支持RTL的编辑器中复制和粘贴评论也行不通(我用记事本测试过了)。

为此,我开发了一个适用于Visual Studio 2013的扩展程序(VirtualRTLtext),它会重新排列一行注释的单词,以便在Visual Studio代码编辑器中显示正确。

重新排列单词对于普通注释很有效,但在XML注释的情况下会有一些问题:

  1. 当您想从注释中创建XML文档时
  2. 当您希望在智能感知中使用您的注释时

对于第一个问题,我找到了部分解决方案,但对于第二个问题,该算法无效(因为智能感知和代码编辑器中的行长度不相等)。

要在Visual Studio中下载它,请转到TOOLS/扩展和更新...并搜索VirtualRTLtext

或者在这里获取vsix表单。

安装后,您可以在VIEW/其他窗口中找到它。

输入图像描述


嘿,Mohammad Fatemipour,我们能拿到你的源代码吗?我有vs2012,但无法安装它。我有Microsoft Visual Studio Ultimate 2012。我遇到了这个错误:VSIXInstaller.NoApplicableSKUsException:此扩展程序无法安装在任何当前安装的产品上。 在VSIXInstaller.App.InitializeInstall()处 在System.Threading.Tasks.Task.InnerInvoke()处 在System.Threading.Tasks.Task.Execute()处 - Eqbal Sohrabi
它在Visual Studio 2022上无法工作。 - arman

5

我最近为vscode写了一个扩展,可以让你从右到左留下评论。你可以在Visual Studio或任何其他程序中使用返回的结果。 "RTL Comment Support" 可以帮助你实现这个功能。


4
你可以在段落前面简单地放置一个RLE(RIGHT-TO-LEFT EMBEDDING, U+202B)字符。这个控制字符也被大多数浏览器和文本编辑器支持。
没有RLE:
Some stuff; // متغیر _value برای نگهداری مقدار اولیه ی کالا می باشد.

使用RLE编码:
Some stuff; // ‫متغیر _value برای نگهداری مقدار اولیه ی کالا می باشد.

使用方法

(我不确定它是否适用于Visual Studio,但是我在VSCode中进行了测试)

您可以复制以下代码用于您的RTL注释(有时在开始输入之前可能需要按下END按钮):

// ‫

也适用于Python或Bash。
# ‫

在VSCode中

在Visual Studio Code中,您需要在设置中设置"editor.renderControlCharacters": false
此外,您还可以为editor.action.toggleRenderControlCharacter设置一个键盘快捷方式。

您可以使用Insert Unicode扩展(并在其中搜索RLE)以简化工作。


3
请将以下与编程有关的内容从英语翻译成中文。只返回翻译后的文本:您可以从此链接https://unicode-table.com/en/复制U+202B. - r.zarei
该方法不适用于Visual Studio 2022。 - arman
@RezaNooralizadeh 我在 Visual Studio 2022 中找不到那个选项。 - arman
1
@HoseinRahnama 你可以将其保存在文件中并放在附近的某个地方,或者使用 "插入 Unicode" 扩展。 - Reza Nooralizadeh
在Windows上还有一个叫做“字符映射”的东西,它非常用户友好且易于使用。 - Hosein Rahnama
显示剩余2条评论

1
根据Microsfot网站的support page,Visual Studio IDE不支持从右到左的书写方向。

客户可以在产品的任何部分使用任何Unicode字符。因此,从右到左的字符是允许的。然而,Visual Studio界面仍然是从左到右的,因此从右到左的显示可能会以不想要的方式重新排列字符。

作为一种方法,您可以在另一个支持RTL的编辑器中编写您的评论,然后将其复制并粘贴到VS Editor中,相当于一种hack。

0
感谢Rouhollah Torshizi,我写了一个简单的HTML页面,可以将混合文本(RTL和LTR)转换为精确格式,您可以像{{link1:示例页面}}一样复制/粘贴。

<!DOCTYPE html>
<html>
<body>

<h2 style="color: red;" class="center">Conver mix text to RTL</h2>
<br/>
<style>
.center {
  margin: auto;
  width: 60%;
   padding: 10px;
}
</style>
<div class="center">
<label>Input Text</label>
<br>
<textarea  style="height: 90px;" id="inputtext" class="center"></textarea>
<br>
<button  class="center "onclick=test()>Conver&Copy</button>
<br/>

<label>Output Text</label>
<br>
<textarea style="height: 90px;" id= "output" class="center"></textarea>
</div>

<p id="demo"></p>
<script>
 const calc=(textValue)=>{
 
     let isRTLFormat = false;
     let virtulArray = [];
     let virtualString = "";
     console.log (textValue);
     let originalTextArray = textValue?.split(' ');
     originalTextArray = originalTextArray?.filter(f => f.trim() != " " && f != '');
     console.log (originalTextArray);
     if (originalTextArray?.length) {
         let temp = [];
         let englishWords= [];
         let numbers = [];
         let numberExist= false;
         for (let i = 0; i < originalTextArray.length; i++) {
             isRTLFormat = isRightToLeft(originalTextArray[i]);
             // If in right to left format
             if (isRTLFormat) {
                 // If a few English words had already been found
                 if (englishWords.length > 1) {
                     temp.unshift(concatItems(englishWords));
                     virtulArray.unshift(...temp);
                     temp = [];
                     englishWords = [];
                 }
                 // If only one English word was found
                 else if (englishWords.length > 0) {
                     temp.unshift(englishWords[0]);
                     virtulArray.unshift(...temp);
                     temp = [];
                     englishWords = [];
                 }
                 // Add right to left word to end of array
                 temp.push(originalTextArray[i]);
             }
             else {
                 // If the word is not number
                 if (!parseInt(originalTextArray[i])) {
                     englishWords.push(originalTextArray[i]);
                     if (numberExist) {
                         temp.unshift(englishWords[0]);
                         virtulArray.unshift(...temp);
                         temp = [];
                         englishWords = [];
                         numberExist = false;
                     }
                 }
                 // If the word is number
                 else {
                     temp.push(originalTextArray[i]);
                     numberExist = true;
                 }
             }
         }
         // Add the last word or words to the array
         if (temp.length > 0) {
             virtulArray.unshift(...temp);
             temp = [];
         }
         if (englishWords.length > 0) {
             temp.unshift(concatItems(englishWords));
             virtulArray.unshift(...temp);
         }
         let concatedArray = [];
         concatedArray = concatItems(virtulArray);
         virtualString = "// " + concatedArray.join(' ');
         console.log(virtualString);
         document.getElementById("output").innerHTML=virtualString;
         var copyText = document.getElementById("output");

  /* Select the text field */
  copyText.select();
 // copyText.setSelectionRange(0, 99999); /* For mobile devices */

  /* Copy the text inside the text field */
  navigator.clipboard.writeText(copyText.value);
  
  /* Alert the copied text */
  alert("Copied the text: " + copyText.value);

         //vscode.env.clipboard.writeText(virtualString);
         // window.showInformationMessage("The text is converted and copied to the clipboard.").then((dismis)=>dismis);

     }
 }
const test=()=>{
var x= document.getElementById('inputtext').value;
calc(x);

}

   function isRightToLeft(text) {
        let isRTL = false;
        const RTL_Regex = /[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]/;
        for (let i = 0; i < text.length; i++) {
            const element = text.charCodeAt(i);
            if (RTL_Regex.test(String.fromCharCode(element))) {
                isRTL = true;
                break
            }
        }
        return isRTL;
    }
    function concatItems(array) {
        let concatedArray=[];
        console.log("concatItems="+array);
        for (let z = 0; z < array.length; z++) {
            concatedArray = concatedArray.concat(array[z]);
        }
        return concatedArray;
    }
</script


</body>
</html>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接