.NET IDE - A Visual Studio Code ExtensionI make videos going through the source code on my youtube channel How to Build this RepositoryExpansion: Minimum Version of Visual Studio CodeThe minimum required VSCode version is 1.68.0 Expansion: Individual Application README.md filesFor succinct to the point 'how to build' documentation go to each application in the repo's README.md individually (following this text are the links to those README.md files)
Expansion: Layout of this README.mdSteps are labeled in the pattern '10.10, 20.10, 30.10, ...' so I don't have to renumber everything when inserted new content.00.10: Be sure to read over the terminal commands found in this README.md before running them. Expansion: Install node.js10.10: Install node.js
10.20: Restart Terminal to refresh environment variables. 10.30: Double check the versions of both 'node', and 'npm'.
Expansion: Scaffolding an Extension20.10: Next is to install the project scaffolder for a Visual Studio Code extension. Microsoft documentation immediately follows this text, following the link I document it myself as well.
20.20: Install the project scaffolder for a Visual Studio Code extension.
20.20: Scaffold a Visual Studio Code extension by running:
20.30: Run the default scaffolded extension and a separate instance of Visual Studio Code should start up with the extension running on that instance only. 20.40: In the Visual Studio Code instance of the running extension hit the following keybind:
20.50: The previous step opens the Visual Studio Code command prompt. The default scaffold of Visual Studio Code extensions comes with a "Hello World!" command. Execute the following command:
20.50: The scaffolded command with id 'dot-net-ide.helloWorld' exists due to the following source code.:
Expansion: Testing an Extension20.50: If the extension is running, stop it as we will now look at how to run the tests.20.60: In the Visual Studio Code sidebar is a tab named "Run and Debug" to help find this tab a gif and some descriptions follow.
20.70: Set the "Run and Debug" sidebar tab as the active tab to view its contents. One can do this by clicking the icon for the "Run and Debug" tab. 20.80: Now that the "Run and Debug" sidebar tab is the active tab, use the dropdown towards the top of the sidebar view to change from 'Run Extension' to 'Extension Tests'. 20.90: Run the 'Extension Tests' 25.10: The directory for the scaffolded extension's tests are located at the following location:
25.20: The location of the file in which one would write there tests is located at the following location:
25.30: Create another test that will fail to showcase what a failed test looks like. Expansion: Adding a Webview25.30: How do we add a webview to the sidebar? The Microsoft documentation for this can be found at the following link:25.40: We can start in '/package.json'. The following bullet points are a list of what we need to change in '/package.json'.
What follows is a JSON snippet containing the changes, and following the JSON snippet is a gif of me making the changes.
25.50: Next we can alter, '/src/extension.ts'. The following bullet points are a list of what we need to change in '/src/extension.ts'.
25.60: As an aside, note that the order of the changes we're making does not matter as we don't compile until fully changing everything. 25.70 Lastly we can add our webview provider file, '/src/UiProviders/SolutionExplorerWebviewProvider.ts'. The following bullet points are a list of what we need to add in '/src/UiProviders/SolutionExplorerWebviewProvider.ts'.
25.80 After creating the file '/src/UiProviders/SolutionExplorerWebviewProvider.ts' go back to file '/src/extension.ts' and add an import so the error goes away. 25.90 I was a bit foolish to use the same webview id as I did when making the actual .NET IDE. Visual Studio Code will crash when running the extension as two webview ids are conflicting. Let's just say I did this on purpose to illustrate the point that webview ids must be unique. We can remedy this by simply changing the webview id and all its references. A gif follows of me doing such. 26.10 Create the folder: '/media'. 26.20 Add the icon that was referenced in package.json to the folder /media.
26.25: When running the extension we can visit the webview in the sidebar. However, we currently have no content. Let's put some text on the webview by altering the file, 'SolutionExplorerWebviewProvider.ts' and the method in that file, 'getWebviewContent(webview: vscode.Webview)'. Expansion: Render Svelte Application in Webview26.30: Let's get started on writing a Svelte application so we can render it in the webview. Any UI framework can be used as long as it outputs javascript at the end of the compilation stage (transpilation stage?).26.40: I have a few quick comments to make before scaffolding the Svelte app. 26.45: I use the following extensions for Svelte in Visual Studio Code
26.50: A video that helped me learn the basic amount of Svelte knowledge I have, and get a Svelte application into a Visual Studio Code webview, is one by a youtuber named, 'Ben Awad'. Find his video at the following link: 26.60: As I mentioned in the previous comment, I have a very basic amount of Svelte knowledge. I've been learning how to use Svelte as I write this extension. Luckily a lot of my .NET knowledge was transferrable to some degree. The same is true when it comes to any TypeScript I wrote for this application. The reason I bring this up is I don't want someone thinking the way I'm doing things are 'correct' or perhaps the phrase 'best practice'. I am putting in all the effort I have to write good code in these languages but, if you see peculiar conventions in regards to Svelte / TypeScript just keep this all in mind. 26.70: Time to scaffold a Svelte application. I am going to make a TypeScript Svelte application using the following documentation: 26.80: The location that the Svelte application is placed I believe is important. From what I understand Visual Studio Code will allow us to 'ignore' the Svelte application source code when we package the extension to be published. This allows for a smaller download size as only the output of the compiled Svelte application would then be included in the published extension. 26.90: In the file '/.vscodeignore' we can ignore the location of the Svelte application. Make the directory '/svelteApps'. Afterwards alter the '/.vscodeignore' file as shown (add 'svelteApps/**'):
27:10: In the terminal change directory to '/svelteApps'. I'll provide the command I used but the command might be different for your setup.
27:20: Now run the terminal commands to scaffold the Svelte application that follow: If you need to download degit: 'npm install -g degit'
You might have to hit the 'Enter' key to send the last command if you are running all 3 in one paste action. 27:30: With the Svelte app created now we need to reference the output of the Svelte compilation step. Inside '/svelteApps/svelte-typescript-app/rollup.config.js' the location of where the output is put is located.
27:40: Change the output file path to be where the Visual Studio Code extension reads from. In otherwords, 'public/build/bundle.js' needs to be changed to '../../out/svelte-typescript-app/svelte-typescript-app.js'
27:50: Now, in the terminal ensure you are in the directory, '/svelteApps/svelte-typescript-app' and then run the following command to install the dependencies (only have to do this once until you next add a dependency):
The following command is used to build the svelte app:
27:60: We can now add a reference to the output of the Svelte application. In the file '/src/UiProviders/SolutionExplorerWebviewProvider.ts' go to the method, 'getWebviewContent(webview: vscode.Webview)'. We need to add references to the output of the Svelte application and render the Svelte app. Alter the 'getWebviewContent(webview: vscode.Webview)' to be the following code snippet:
We also must modify the method, 'resolveWebviewView(webviewView: vscode.WebviewView)'. The modification is to allow for scripts in the Webview. Within the options for the webview (webviewView.webview.options) add another property, 'enableScripts: true'. Without this the Svelte app cannot run.
27:70: Upon running the extension we now see the Svelte application. 27:80: Let's add a counter button to the Svelte application as shown in the following gif. 27:90: And next we need to run the command to build the svelte application. Afterwards, run the extension and click the button and watch it increment.
27:90: We should make the css of the Svelte application match Visual Studio Code's css. Let's do that now. Add to '/media' the following two files.
Within those two files paste the respective file contents that are in the collapsed section below as to not take up too much space (click it to expand the collapsed section). Expand to see '/media/reset.css'``` css html { box-sizing: border-box; font-size: 13px; }*, *:before, *:after { box-sizing: inherit; } body, h1, h2, h3, h4, h5, h6, p, ol, ul { margin: 0; padding: 0; font-weight: normal; } img { max-width: 100%; height: auto; }
28:10 Now we can alter the file, '/src/UiProviders/SolutionExplorerWebviewProvider.ts' and the method within that file, 'getWebviewContent(webview: vscode.Webview)'
28:10 Build the Svelte application and then run it. The button now appears with Visual Studio Code's css applied.
28:20 And that's the end of me building the extension again from scratch. The Nuget Package Manager works in the same way as the Solution Explorer from a Visual Studio Code extension webview standpoint. The actual Svelte code is not the same of course. |