Added Swagger
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import snippets from "./snippets"
|
||||
import getSnippetsForPath from "./get-snippets-for-path"
|
||||
|
||||
export default function getCompletions(editor, session, pos, prefix, cb, ctx, system) {
|
||||
|
||||
const { fn: { getPathForPosition }, specSelectors } = system
|
||||
const { isOAS3 } = specSelectors
|
||||
|
||||
if(isOAS3 && isOAS3()) {
|
||||
// isOAS3 selector exists, and returns true
|
||||
return cb(null, null)
|
||||
}
|
||||
|
||||
const { AST } = ctx
|
||||
const editorValue = editor.getValue()
|
||||
const path = getPathForPosition({ pos, prefix, editorValue, AST})
|
||||
|
||||
const suggestions = getSnippetsForPath({ path, snippets})
|
||||
|
||||
return cb(null, suggestions)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import isArray from "lodash/isArray"
|
||||
|
||||
export default function getSnippetsForPath({ path, snippets }) {
|
||||
// find all possible snippets, modify them to be compatible with Ace and
|
||||
// sort them based on their position. Sorting is done by assigning a score
|
||||
// to each snippet, not by sorting the array
|
||||
if (!isArray(path)) {
|
||||
return []
|
||||
}
|
||||
|
||||
return snippets
|
||||
.filter(snippet => {
|
||||
return snippet.path.length === path.length
|
||||
})
|
||||
.filter(snippet => {
|
||||
return snippet.path.every((k, i) => {
|
||||
return !!(new RegExp(k)).test(path[i])
|
||||
})
|
||||
})
|
||||
.map(snippet => {
|
||||
// change shape of snippets for ACE
|
||||
return {
|
||||
caption: snippet.name,
|
||||
snippet: snippet.content,
|
||||
meta: "snippet"
|
||||
}
|
||||
})
|
||||
.map(snippetSorterForPos(path))
|
||||
}
|
||||
|
||||
export function snippetSorterForPos(path) {
|
||||
return function(snippet) {
|
||||
// by default score is high
|
||||
let score = 1000
|
||||
|
||||
// if snippets content has the keyword it will get a lower score because
|
||||
// it's more likely less relevant
|
||||
// (FIX) is this logic work for all cases?
|
||||
path.forEach(function(keyword) {
|
||||
if (snippet.snippet.indexOf(keyword)) {
|
||||
score = 500
|
||||
}
|
||||
})
|
||||
|
||||
snippet.score = score
|
||||
|
||||
return snippet
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import * as wrapActions from "./wrap-actions"
|
||||
|
||||
export default function EditorAutosuggestSnippetsPlugin() {
|
||||
return {
|
||||
statePlugins: {
|
||||
editor: {
|
||||
wrapActions,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
const operationRegex = "get|put|post|delete|options|head|patch"
|
||||
|
||||
/**
|
||||
* Makes an HTTP operation snippet's content based on operation name
|
||||
*
|
||||
* @param {string} operationName - the HTTP verb
|
||||
*
|
||||
* @return {string} - the snippet content for that operation
|
||||
*/
|
||||
function makeOperationSnippet(operationName) {
|
||||
return [
|
||||
"${1:" + operationName + "}:",
|
||||
" summary: ${2}",
|
||||
" description: ${2}",
|
||||
" responses:",
|
||||
" ${3:200:}",
|
||||
" description: ${4:OK}",
|
||||
"${6}"
|
||||
].join("\n")
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an HTTP response code snippet's content based on code
|
||||
*
|
||||
* @param {string} code - HTTP Response Code
|
||||
*
|
||||
* @return {string} - Snippet content
|
||||
*/
|
||||
function makeResponseCodeSnippet(code) {
|
||||
return [
|
||||
"${1:" + code + "}:",
|
||||
" description: ${2}",
|
||||
"${3}"
|
||||
].join("\n")
|
||||
}
|
||||
|
||||
export default [
|
||||
{
|
||||
name: "swagger",
|
||||
trigger: "sw",
|
||||
path: [],
|
||||
content: [
|
||||
"swagger: \'2.0\'",
|
||||
"${1}"
|
||||
].join("\n")
|
||||
},
|
||||
|
||||
{
|
||||
name: "info",
|
||||
trigger: "info",
|
||||
path: [],
|
||||
content: [
|
||||
"info:",
|
||||
" version: ${1:0.0.0}",
|
||||
" title: ${2:title}",
|
||||
" description: ${3:description}",
|
||||
" termsOfService: ${4:terms}",
|
||||
" contact:",
|
||||
" name: ${5}",
|
||||
" url: ${6}",
|
||||
" email: ${7}",
|
||||
" license:",
|
||||
" name: ${8:MIT}",
|
||||
" url: ${9:http://opensource.org/licenses/MIT}",
|
||||
"${10}"
|
||||
].join("\n")
|
||||
},
|
||||
|
||||
{
|
||||
name: "get",
|
||||
trigger: "get",
|
||||
path: ["paths", "."],
|
||||
content: makeOperationSnippet("get")
|
||||
},
|
||||
|
||||
{
|
||||
name: "post",
|
||||
trigger: "post",
|
||||
path: ["paths", "."],
|
||||
content: makeOperationSnippet("post")
|
||||
},
|
||||
|
||||
{
|
||||
name: "put",
|
||||
trigger: "put",
|
||||
path: ["paths", "."],
|
||||
content: makeOperationSnippet("put")
|
||||
},
|
||||
|
||||
{
|
||||
name: "delete",
|
||||
trigger: "delete",
|
||||
path: ["paths", "."],
|
||||
content: makeOperationSnippet("delete")
|
||||
},
|
||||
|
||||
{
|
||||
name: "patch",
|
||||
trigger: "patch",
|
||||
path: ["paths", "."],
|
||||
content: makeOperationSnippet("patch")
|
||||
},
|
||||
|
||||
{
|
||||
name: "options",
|
||||
trigger: "options",
|
||||
path: ["paths", "."],
|
||||
content: makeOperationSnippet("options")
|
||||
},
|
||||
|
||||
// operation level parameter
|
||||
{
|
||||
name: "parameter",
|
||||
trigger: "param",
|
||||
path: ["paths", ".", ".", "parameters"],
|
||||
content: [
|
||||
"- name: ${1:parameter_name}",
|
||||
" in: ${2:query}",
|
||||
" description: ${3:description}",
|
||||
" type: ${4:string}",
|
||||
"${5}"
|
||||
].join("\n")
|
||||
},
|
||||
|
||||
// path level parameter
|
||||
{
|
||||
name: "parameter",
|
||||
trigger: "param",
|
||||
path: ["paths", ".", "parameters"],
|
||||
content: [
|
||||
"- name: ${1:parameter_name}",
|
||||
" in: ${2:path}",
|
||||
" required: true",
|
||||
" description: ${3:description}",
|
||||
" type: ${4:string}",
|
||||
"${5}"
|
||||
].join("\n")
|
||||
},
|
||||
|
||||
{
|
||||
name: "response",
|
||||
trigger: "resp",
|
||||
path: ["paths", ".", ".", "responses"],
|
||||
content: [
|
||||
"${1:code}:",
|
||||
" description: ${2}",
|
||||
" schema: ${3}",
|
||||
"${4}"
|
||||
].join("\n")
|
||||
},
|
||||
|
||||
{
|
||||
name: "200",
|
||||
trigger: "200",
|
||||
path: ["paths", ".", operationRegex, "responses"],
|
||||
content: makeResponseCodeSnippet("200")
|
||||
},
|
||||
|
||||
{
|
||||
name: "300",
|
||||
trigger: "300",
|
||||
path: ["paths", ".", operationRegex, "responses"],
|
||||
content: makeResponseCodeSnippet("300")
|
||||
},
|
||||
|
||||
{
|
||||
name: "400",
|
||||
trigger: "400",
|
||||
path: ["paths", ".", operationRegex, "responses"],
|
||||
content: makeResponseCodeSnippet("400")
|
||||
},
|
||||
|
||||
{
|
||||
name: "500",
|
||||
trigger: "500",
|
||||
path: ["paths", ".", operationRegex, "responses"],
|
||||
content: makeResponseCodeSnippet("500")
|
||||
},
|
||||
|
||||
{
|
||||
name: "model",
|
||||
trigger: "mod|def",
|
||||
regex: "mod|def",
|
||||
path: ["definitions"],
|
||||
content: [
|
||||
"${1:ModelName}:",
|
||||
" type: object",
|
||||
" properties:",
|
||||
" ${2}"
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,11 @@
|
||||
import getCompletions from "./get-completions"
|
||||
|
||||
// Add an autosuggest completer
|
||||
export const addAutosuggestionCompleters = (ori, system) => (context) => {
|
||||
return ori(context).concat([{
|
||||
getCompletions(...args) {
|
||||
// Add `context`, then `system` as the last args
|
||||
return getCompletions(...args, context, system)
|
||||
}
|
||||
}])
|
||||
}
|
||||
Reference in New Issue
Block a user