Skip to content

Commit

Permalink
feat[code-snippets]: add code snippet generation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-gupta-ij committed Dec 18, 2024
1 parent 5a2de1b commit ca0f6b6
Show file tree
Hide file tree
Showing 5 changed files with 6,778 additions and 4,847 deletions.
50 changes: 50 additions & 0 deletions src/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,54 @@ export class OpenapiAutocomplete {

return result;
}

getSnippets() {
let completions = [];
for (let method of this.methods) {
let completionCount = 1;
let insertText = `${method.method.toUpperCase()} `;

const parameters = method.parameters;
if (parameters) {
for (let param of parameters) {
if (param.in === "path" && param.required) {
const regex = new RegExp(`\\{${param.name}\\}`, "g");
method.path = method.path.replace(
regex,
`\${${completionCount}:${param.name}}`
);
completionCount++;
}
}
}
insertText += method.path;

const dataRef = method.body;
if (dataRef) {
let current = this.extractor.objectByRef(dataRef);
if (current.required && current.required.length > 0) {
insertText += "\n{";
for (let i = 0; i < current.required.length; i++) {
const key = current.required[i];
insertText += `\n "${key}": $${completionCount}`;
completionCount++;
if (i < current.required.length - 1) {
insertText += ",";
}
}
insertText += "\n}";
}
}

completions.push({
label: method.operationId,
documentation: method.summary,
insertText: insertText,
kind: 1,
insertTextRules: 4,
});
}

return completions;
}
}
8 changes: 6 additions & 2 deletions src/openapi-extractor.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export class OpenAPIMethod {
constructor(method, path, body, operationId, tags) {
constructor(method, path, body, operationId, tags, parameters,summary) {
this.method = method;
this.path = path;
this.body = body; // definition of the body
this.operationId = operationId;
this.tags = tags;
this.parameters = parameters;
this.summary = summary;
}
}

Expand All @@ -26,7 +28,9 @@ export class OpenAPIExtractor {
"application/json"
]?.schema?.["$ref"],
this.openapi.paths[path][method].operationId,
this.openapi.paths[path][method].tags
this.openapi.paths[path][method].tags,
this.openapi.paths[path][method]?.parameters,
this.openapi.paths[path][method].summary
)
);
}
Expand Down
54 changes: 54 additions & 0 deletions tests/autocompleteSnippets.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use strict";

import assert from "assert";

import openapi from "./data/openapi.json" assert { type: "json" };

import { OpenapiAutocomplete } from "../src/autocomplete.js";
import { OpenAPIExtractor } from "../src/openapi-extractor.js";

describe("Snippets", () => {
const extractor = new OpenAPIExtractor(openapi);

const methods = extractor.getAllMethods();
let theAuto = new OpenapiAutocomplete(openapi, []);

const snippets = theAuto.getSnippets();

it("should return snippets for all methods", () => {
assert.ok(snippets.length === methods.length);
});

it("should return snippets with correct label", () => {
for (let i = 0; i < snippets.length; i++) {
assert.ok(snippets[i].label === methods[i].operationId);
}
});

it("should return snippets with correct documentation", () => {
for (let i = 0; i < snippets.length; i++) {
assert.ok(snippets[i].documentation === methods[i].summary);
}
});

it("should return snippets with correct insertText", () => {
let search_matrix_pairs = snippets.find(
(s) => s.label === "search_matrix_pairs"
);
assert.ok(
search_matrix_pairs.insertText ===
"POST /collections/${1:collection_name}/points/search/matrix/pairs"
);

let query_points_groups = snippets.find(
(s) => s.label === "query_points_groups"
);
assert.ok(
query_points_groups.insertText ===
"POST /collections/${1:collection_name}/points/query/groups\n" +
"{\n" +
' "group_by": $2\n' +
"}"
);
});
});
Loading

0 comments on commit ca0f6b6

Please sign in to comment.