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

feat: add in conditional operator sample #414

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions in-conditional-operator-sample/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**@type {import('eslint').Linter.Config} */
// eslint-disable-next-line no-undef
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {
'semi': [2, "always"],
'@typescript-eslint/no-unused-vars': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/no-non-null-assertion': 0,
}
};
9 changes: 9 additions & 0 deletions in-conditional-operator-sample/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp

// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"dbaeumer.vscode-eslint"
]
}
22 changes: 22 additions & 0 deletions in-conditional-operator-sample/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// A launch configuration that compiles the extension and then opens it inside a new window
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [
{
"name": "Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "npm: watch"
}
]
}
3 changes: 3 additions & 0 deletions in-conditional-operator-sample/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.insertSpaces": false
}
20 changes: 20 additions & 0 deletions in-conditional-operator-sample/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
40 changes: 40 additions & 0 deletions in-conditional-operator-sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# `in` Conditional Operator Sample

This example demonstrates the usage of the "when clause" [`in` conditional operator`](https://code.visualstudio.com/api/references/when-clause-contexts#in-conditional-operator)
To create **dynamic** context menus based on **custom** provided logic.

In this example we have two context menus entries when right-clicking on `package.json` files.
These entries should only be displayed to the end-user under specific conditions:
- `Install Dependencies` should only appear if the **specific** `package.json` file includes
a dependencies or devDependencies section.
- `Run Tests` should only appear if the **specific** `package.json` includes a `scripts.test` section.

## Demo

![demo](./demo.gif)

## VS Code API

### `vscode` module

- [`workspace.findFiles`](https://code.visualstudio.com/api/references/vscode-api#workspace.findFIles)
- [`workspace.createFileSystemWatcher`](https://code.visualstudio.com/api/references/vscode-api#workspace.createFileSystemWatcher)
- [`commands.registerCommand`](https://code.visualstudio.com/api/references/vscode-api#commands.registerCommand)
- [`commands.executeCommand`](https://code.visualstudio.com/api/references/vscode-api#commands.executeCommand)
- [`tasks.executeTask`](https://code.visualstudio.com/api/references/vscode-api#tasks.executeTask)

### Contribution Points

- [`contributes.commands`](https://code.visualstudio.com/api/references/contribution-points#contributes.commands)
- [`contributes.menus`](https://code.visualstudio.com/api/references/contribution-points#contributes.menus)


## Running the Sample

- Run `npm install` in terminal to install dependencies.
- Run `npm compile` in terminal to compile the sources.
- Run the `Run Extension` target in the Debug View this will run the extension in a new VS Code window.
- `file` --> `Open Folder` --> [`example-workspace`](./example-workspace) in the new VS Code window.
- Wait a few seconds for the file watcher to initialize the context state.
- Right click on the four different `package.json` files available in that folder and
note how the `Install Dependencies` and `Run Tests` context menus only appear where relevant.
Binary file added in-conditional-operator-sample/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions in-conditional-operator-sample/example-workspace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules

# These are part of the demo flow
# no need to pollute the source control
package-lock.json

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "with-deps",
"version": "1.0.0",
"description": "Example of a package.json with both `dependencies` and `test script` sections",
"dependencies": {
"lodash": "^4.17.21"
},
"scripts": {
"test": "echo \"hello cruel world!\""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "with-deps",
"version": "1.0.0",
"description": "Example of a package.json with `dependencies` but without `test script` sections",
"dependencies": {
"lodash": "^4.17.21"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "with-deps",
"version": "1.0.0",
"description": "Example of a package.json without both `dependencies` and `test script` sections",
"scripts": {
"build" : "echo \"compiling...\""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "with-scripts",
"version": "1.0.0",
"description": "Example of a package.json with `test script` but without `dependencies` sections",
"scripts": {
"test" : "echo \"hello cruel world!\""
}
}
Loading