Skip to content

Commit

Permalink
Merge branch 'context-menu-open'
Browse files Browse the repository at this point in the history
  • Loading branch information
wozjac committed Jun 21, 2021
2 parents ee5670e + 62deda2 commit c250023
Show file tree
Hide file tree
Showing 21 changed files with 524 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
coverage
.vscode-test
*.vsix
pnpm-lock.yaml
10 changes: 4 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [{
"configurations": [
{
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}"
]
"args": ["--disable-extensions", "--extensionDevelopmentPath=${workspaceFolder}"]
},
{
"name": "Extension Tests",
Expand All @@ -24,4 +22,4 @@
]
}
]
}
}
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
"eslint.options": {
"configFile": "./config/.eslintrc.json"
},
"beautify.config": "./config/.jsbeautifyrc"
"beautify.config": "./config/.jsbeautifyrc",
"prettier.configPath": "./config/.prettierrc",
"eslint.codeAction.showDocumentation": {

"enable": true
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ API side bar can be also opened using command:
- members can be also filtered by a search term, for example to display only methods with "add", type "hbox ?madd"
<img src="https://publicrepo.vipserv.org/images/vscode-api/search-members2.gif" alt="basic search" width="250px"/>

### XML views
API can be also opened for controls in XML views; use the context menu option on a selected control:
<img src="https://publicrepo.vipserv.org/images/vscode-api/context.gif" alt="context menu" width="650px"/>

## Extension Settings
This extension contributes the following settings:

Expand Down
15 changes: 6 additions & 9 deletions config/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
"no-unused-vars": "warn",
"constructor-super": "warn",
"valid-typeof": "warn",
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
]
"prefer-const": "error",
"no-var": "error",
"arrow-body-style": ["error", "always"],
"quotes": ["warn", "double"],
"semi": ["error", "always"]
}
}
}
4 changes: 2 additions & 2 deletions config/.jsbeautifyrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"html": {
"indent_size": 2,
"end_with_newline": true,
"wrap_attributes": "force"
"wrap_attributes": "auto"
},
"xml": {
"indent_size": 2,
"end_with_newline": true,
"wrap_attributes": "force"
"wrap_attributes": "auto"
},
"js": {
"indent_size": 2,
Expand Down
5 changes: 5 additions & 0 deletions config/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 100,
"singleQuote": false,
"arrowParens": "always"
}
33 changes: 26 additions & 7 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const APIReferenceProvider = require("./src/view/APIReferenceProvider");
const ui5ApiService = require("./src/core/ui5APIService");
const dataSource = require("./src/core/dataSource.js");
const constants = require("./src/core/constants.js");
const contextMenu = require("./src/core/contextMenu");

function activate(context) {
const templatePaths = getTemplatePaths(context.extensionUri);
Expand All @@ -11,24 +12,42 @@ function activate(context) {
const apiViewProvider = new APIReferenceProvider(context.extensionUri, templates);

context.subscriptions.push(
vscode.window.registerWebviewViewProvider("ui5ApiReferenceView", apiViewProvider, { webviewOptions: { retainContextWhenHidden: true } }));
vscode.window.registerWebviewViewProvider("ui5ApiReferenceView", apiViewProvider, {
webviewOptions: {
retainContextWhenHidden: true,
},
})
);

context.subscriptions.push(
vscode.commands.registerCommand("vscode-ui5-api-reference.showAPIView", async () => {
await vscode.commands.executeCommand("workbench.view.extension.ui5ApiReference");
}));
})
);

context.subscriptions.push(
vscode.commands.registerCommand("vscode-ui5-api-reference.showAPIViewForValue", async () => {
const editor = vscode.window.activeTextEditor;
// Gets the entire string from namespace and control
const input = editor.selection !== "" ? contextMenu.findControl(editor) : editor.selection;
await vscode.commands.executeCommand("workbench.view.extension.ui5ApiReference");
apiViewProvider.triggerSearchCommand(input);
})
);

context.subscriptions.push(
vscode.commands.registerCommand("vscode-ui5-api-reference.searchAPI", async () => {
const input = await vscode.window.showInputBox();
await vscode.commands.executeCommand("workbench.view.extension.ui5ApiReference");
apiViewProvider.triggerSearchCommand(input);
}));
})
);

const apiBaseUrl = configuration.get("apiURL");
ui5ApiService.setAPIBaseURL(apiBaseUrl);

ui5ApiService.loadUi5Objects()
ui5ApiService
.loadUi5Objects()
.then(() => {
return ui5ApiService.loadUi5LibrariesDesignApi();
})
Expand All @@ -41,10 +60,10 @@ function getTemplatePaths(extensionUri) {
return {
webview: vscode.Uri.joinPath(extensionUri, "src/view/templates", "webview.html"),
members: vscode.Uri.joinPath(extensionUri, "src/view/templates", "members.html"),
objectAPI: vscode.Uri.joinPath(extensionUri, "src/view/templates", "objectAPI.html")
objectAPI: vscode.Uri.joinPath(extensionUri, "src/view/templates", "objectAPI.html"),
};
}

module.exports = {
activate
};
activate,
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@
],
"main": "./extension.js",
"scripts": {
"coverage": "c8 npm test",
"lint": "eslint -c config/.eslintrc.json --ignore-path config/.eslintignore ./src/**/*.js",
"coveralls": "c8 npm test && c8 report --reporter=text-lcov | coveralls",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"pretest": "npm run lint",
"test": "mocha",
"test": "node ./test/runTest.js",
"release": "standard-version"
},
"devDependencies": {
Expand Down Expand Up @@ -76,8 +75,23 @@
{
"command": "vscode-ui5-api-reference.searchAPI",
"title": "VSCode-UI5: display API for..."
},
{
"command": "vscode-ui5-api-reference.showAPIViewForValue",
"title": "VSCode-UI5: Show API for selection",
"category": "navigation",
"where": "editorHasSelection"
}
],
"menus": {
"editor/context": [
{
"when": "resourceLangId == xml",
"command": "vscode-ui5-api-reference.showAPIViewForValue",
"group": "navigation"
}
]
},
"views": {
"ui5ApiReference": [
{
Expand Down
86 changes: 86 additions & 0 deletions src/core/contextMenu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//Formats the namespace so we can use it for search
const getNamespace = (text, editor) => {
const namespace = text.includes(":") ? text.split(":")[0] : "";
text = text.includes(":") ? text.split(":")[1] : text;

let xmlns = editor.document
.getText()
.split("\n")
.find((line) => {
return namespace === ""
? line.includes("xmlns=")
: line.includes("xmlns") && line.includes(namespace);
})
.trim();

xmlns = xmlns.includes(">") ? xmlns.slice(0, -1) : xmlns;
const regex = new RegExp(namespace === "" ? `xmlns="(.*?)"` : `xmlns:${namespace}="(.*?)\"`);
xmlns = xmlns.match(regex)[0].match(/\"(.*?)\"/)[1];

return `${xmlns}.${text}`;
};

const findControl = (editor) => {
const isLowerCase = (string) => {
return /^[a-z]*$/.test(string);
};

const cursorPosition = editor.selection.active;
let line = editor.document.lineAt(cursorPosition.line);
let text = line.text.trim();

if (text.includes("<")) {
//If the first character after the < is lowercase, then it's an aggregation
if (
isLowerCase(text.charAt(1) === "/" ? text.charAt(2) : text.charAt(1)) &&
isLowerCase(text.indexOf(":") === -1 ? true : text.charAt(text.indexOf(":") + 1))
) {
let foundControl = false;
let count = 1;
const navigator = text.charAt(1) === "/" ? "-" : "+";

while (foundControl === false) {
count = navigator === "+" ? cursorPosition.line - count : cursorPosition.line + count;
line = editor.document.lineAt(count);
text = line.text.trim();
if (
text.includes("<") &&
!isLowerCase(text.charAt(1) === "/" ? text.charAt(2) : text.charAt(1))
) {
foundControl = true;
} else {
count++;
}
}
}

//just for good measure, check if it's the end tag
const tagSplit = text.includes("</") ? "</" : "<";
const endSplit = text.includes(" ") ? " " : text.includes(">") ? ">" : "\n";
text = text.split(tagSplit)[1].split(endSplit)[0];
} else {
let count = cursorPosition.line - 1;
text = "";

while (!text) {
line = editor.document.lineAt(count);
text = line.text.trim();

if (text.includes("<")) {
const endSplit = text.includes(">") ? ">" : text.includes(" ") ? " " : "\n";
text = text.split("<")[1].split(endSplit)[0];
} else {
count = count - 1;
}
}
}

text = text.includes(">") ? text.slice(0, -1) : text;

return getNamespace(text, editor);
};

module.exports = {
getNamespace,
findControl,
};
22 changes: 22 additions & 0 deletions test/runTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require("path");
const { runTests } = require("vscode-test");

async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, "../");

// The path to the extension test script
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, "./suite/index");

// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
} catch (err) {
console.error("Failed to run tests");
process.exit(1);
}
}

main();
59 changes: 59 additions & 0 deletions test/suite/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const path = require("path");
const Mocha = require("mocha");
const glob = require("glob");

const doCoverage = true;

function setupCoverage() {
const NYC = require("nyc");

const nyc = new NYC({
cwd: path.join(__dirname, "..", ".."),
exclude: ["**/test/**", ".vscode-test/**"],
reporter: ["lcov"],
all: true,
instrument: true,
hookRequire: true,
hookRunInContext: true,
hookRunInThisContext: true,
});

nyc.reset();
nyc.wrap();

return nyc;
}

async function run() {
const nyc = doCoverage ? setupCoverage() : null;

const mocha = new Mocha({
ui: "bdd",
color: true,
timeout: 10 * 1000,
});

const testsRoot = path.resolve(__dirname, "..");
const files = glob.sync("**/*.test.js", { cwd: testsRoot });

files.forEach((file) => {
mocha.addFile(path.resolve(testsRoot, file));
});

try {
await new Promise((resolve, reject) => {
mocha.run((failures) => {
failures ? reject(new Error(`${failures} tests failed`)) : resolve(undefined);
});
});
} finally {
if (nyc) {
nyc.writeCoverageFile();
await nyc.report();
}
}
}

module.exports = {
run,
};
Loading

0 comments on commit c250023

Please sign in to comment.