Hosein Masoomi
Hosein Masoomi

Reputation: 197

How to comment in a Right to left language in Visual Studio IDE

I want to comment in visual studio in Persian (which is a right to left language) like this:

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

But it seems the code editor does not support RTL languages and reorders the words so the result is:

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

That does not read as I expected!

Anybody have a solution for this problem?

Upvotes: 18

Views: 7159

Answers (5)

Reza Alizade
Reza Alizade

Reputation: 328

You can simply put a RLE (RIGHT-TO-LEFT EMBEDDING, U+202B) character before your paragraph. This control character is also supported in browsers and most text editors.

Without RLE:

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

With RLE:

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

How to use

(I'm not sure whether it works in Visual Studio or not, BTW I've tested it in VSCode)

You can copy below codes for your RTL comments (you may sometimes have to press END button before start typing):

// ‫

Also for Python or Bash:

# ‫

In VSCode

In Visual Studio Code, you have to set "editor.renderControlCharacters": false in settings.
Also you can set a keyboard shortcut for editor.action.toggleRenderControlCharacter.

You can use Insert Unicode extension (and search for RLE inside it) to make the work simpler.

Upvotes: 4

masoud_ALA
masoud_ALA

Reputation: 1

thanks to Rouhollah Torshizi i wrote a simple html page that convert mix text(RTL&LTR) to exact format you can copy/past like sample page

<!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>

Upvotes: 0

Rouhollah Torshizi
Rouhollah Torshizi

Reputation: 161

I recently wrote an extension for vscode that allows you to leave comments from right to left. You can use the result that returns, in Visual Studio or any other program. "RTL Comment Support" do it for you.

Upvotes: 5

Ahmad
Ahmad

Reputation: 9678

According this support page from Microsfot website the right to left is not supported in Visual Studio IDE

Customers can use any Unicode character in any part of the product. So certainly Right-To-Left characters are allowed. However, the Visual Studio interface is still Left-To-Right therefore the RTL display may re-arrange the characters in an unwanted manner.

As a hack you can write your comment in another editor that supports RTL, then copy and paste it into the VS Editor.

Upvotes: 1

Mohammad Fatemipour
Mohammad Fatemipour

Reputation: 154

As Ahmad mentioned Microsoft does not support Right-to-left in its Visual Studio IDE (unfortunately), to copy and paste the comment form a RTL-supported editor also does not work (I test it with Notepad)

For this purpose, I have developed an extension for visual studio 2013 (VirtualRTLtext), it rearranges the words of a line of comment so that it will looks correct in Visual Studio code editor.

Rearranging words works well for usual comments but in case of XML comment it has some problems:

  1. When you want to make XML documentation from your comments
  2. When you want your comments in intellisense

For the first, i could find a partial solution but for the second, this algorithm does not work (because length of lines in intellisense and code editor is not equal).

To download it in Visual Studio go to TOOLS/extension and updates... and search VirtualRTLtext

Or get the vsix form Here.

After installation you could find it in VIEW/Other Windows

enter image description here

Upvotes: 13

Related Questions