Skip to content

Commit

Permalink
Finished adapting completion logic and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grolu committed Sep 9, 2024
1 parent 9a16e96 commit f067c4e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
76 changes: 48 additions & 28 deletions frontend/__tests__/composables/useShootEditor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('composables', () => {
let shootEditorCompletions
let createMockCompletionContext

beforeAll(() => {
beforeEach(() => {
const element = document.createElement('div')
const state = EditorState.create({
extensions: [
Expand Down Expand Up @@ -264,36 +264,36 @@ describe('composables', () => {
describe('#editorTooltip', () => {
it('should return a simple tooltip', () => {
setEditorContentAndCursor('spec:', 1, 0)
editorView.coordsChar = () => {
return { line: 0, ch: 1 }
editorView.posAtCoords = () => {
return 1 // { line: 1, ch: 1 }
}
const tooltip = shootEditorCompletions.editorTooltip({}, editorView)
expect(tooltip.detail).toBe('Object')
})

it('should return tooltips for nested properties', () => {
setEditorContentAndCursor('spec:\n metadata:\n managedFields:\n', 1, 0)
editorView.coordsChar = () => {
return { line: 1, ch: 1 }
editorView.posAtCoords = () => {
return 6 // { line: 1, ch: 1 }
}
let tooltip = shootEditorCompletions.editorTooltip({}, editorView)
expect(tooltip).toBeUndefined()

editorView.coordsChar = () => {
return { line: 1, ch: 3 }
editorView.posAtCoords = () => {
return 8 // { line: 1, ch: 3 }
}
tooltip = shootEditorCompletions.editorTooltip({}, editorView)
expect(tooltip.detail).toBe('Object')

editorView.coordsChar = () => {
return { line: 2, ch: 5 }
editorView.posAtCoords = () => {
return 23 // { line: 2, ch: 5 }
}
tooltip = shootEditorCompletions.editorTooltip({}, editorView)
expect(tooltip.detail).toBe('Array')
expect(tooltip.info).toBe('Demo Array')

editorView.coordsChar = () => {
return { line: 2, ch: 18 }
editorView.posAtCoords = () => {
return 36 // { line: 2, ch: 18 }
}
tooltip = shootEditorCompletions.editorTooltip({}, editorView)
expect(tooltip).toBeUndefined()
Expand All @@ -302,46 +302,66 @@ describe('composables', () => {

describe('#editorEnter', () => {
let spy
beforeEach(() => {
spy = vi.spyOn(editorView, 'replaceSelection')
})
const setupSpy = () => {
spy = vi.spyOn(editorView, 'dispatch')
}

afterEach(() => {
spy.mockReset()
})

it('should return a simple line break', () => {
setEditorContentAndCursor('', 1, 0)
setupSpy()
shootEditorCompletions.editorEnter(editorView)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith('\n')
expect(spy).toHaveBeenCalledWith({
changes: { from: 0, to: 0, insert: '\n' },
selection: { anchor: 1 },
})

spy.mockReset()
setEditorContentAndCursor('test', 1, 0)
spy.mockReset()
shootEditorCompletions.editorEnter(editorView)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith('\n')
expect(spy).toHaveBeenCalledWith({
changes: { from: 0, to: 0, insert: '\n' },
selection: { anchor: 1 },
})
})

it('should preserve indent after a regular line', () => {
setEditorContentAndCursor('spec:\n foo:bar', 2, 9)
setupSpy()
shootEditorCompletions.editorEnter(editorView)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith('\n ')
expect(spy).toHaveBeenCalledWith({
changes: { from: 15, to: 15, insert: '\n ' },
selection: { anchor: 18 },
})
})

it('should increase indent after an object or array', () => {
setEditorContentAndCursor('spec:\n foo:', 2, 6)
setupSpy()
shootEditorCompletions.editorEnter(editorView)
const indent = editorView.state.facet(indentUnit).length
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith(`\n ${repeat(' ', editorView.options.indentUnit)}`)
expect(spy).toHaveBeenCalledWith({
changes: { from: 12, to: 12, insert: `\n ${repeat(' ', indent)}` },
selection: { anchor: 18 },
})
})

it('should increase indent after first item of an array', () => {
setEditorContentAndCursor('spec:\n foo:\n - foo:bar', 3, 13)
setupSpy()
shootEditorCompletions.editorEnter(editorView)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith('\n ')
expect(spy).toHaveBeenCalledWith({
changes: { from: 26, to: 26, insert: '\n ' },
selection: { anchor: 33 },
})
})
})
})
Expand Down Expand Up @@ -418,13 +438,13 @@ describe('composables', () => {
})
describe('#resolveShemaArrays', () => {
it('should recursively remove allOf, anyOf and oneOf array level', () => {
const shootEditorCompletions = new EditorCompletions(shootCompletions)
const shootEditorCompletionsV3 = new EditorCompletions(shootCompletionsV3)
const shootEditorCompletions = new EditorCompletions(shootCompletions, { cmView: editorView })
const shootEditorCompletionsV3 = new EditorCompletions(shootCompletionsV3, { cmView: editorView })
expect(shootEditorCompletionsV3.shootCompletions).toEqual(shootEditorCompletions.shootCompletions)
})

it('should handle multiple type options using oneOf discriminators', () => {
// add multi-type value to openapi v3 spec (not supported by v2)
// add multi-type value to openapi v3 spec (not supported by v2)
shootCompletionsV3.spec.allOf[0].properties.foo = {
oneOf: [
{
Expand All @@ -438,15 +458,15 @@ describe('composables', () => {
}

const shootEditorCompletionsV3 = new EditorCompletions(shootCompletionsV3, {
cm: editor,
cmView: editorView,
})
setEditorContentAndCursor('spec:\n ', 1, 3)
setEditorContentAndCursor('spec:\n ', 2, 3)
const completions = shootEditorCompletionsV3.yamlHint(createMockCompletionContext(editorView)).options
expect(completions).toHaveLength(4)

const { text, type } = completions[3]
expect(text).toBe('foo: ')
expect(type).toBe('Number (int32) | String')
const { apply, detail } = completions[3]
expect(apply).toBe('foo: ')
expect(detail).toBe('Number (int32) | String')
})
})
})
Expand Down
5 changes: 1 addition & 4 deletions frontend/src/composables/useShootEditor/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import { indentUnit } from '@codemirror/language'
import { useLogger } from '@/composables/useLogger'

import {
forEach,
join,
map,
trim,
nth,
filter,
Expand Down Expand Up @@ -312,7 +309,7 @@ export class EditorCompletions {
return
}

const [, indent, propertyName] = lineString.match(/^(\s*)(.+?):$/)
const [, indent, propertyName] = lineString.match(/^(\s*)(?:-\s)?(.+?):$/)
const token = { string: lineString }
token.indent = token.start = indent.length
token.propertyName = propertyName
Expand Down

0 comments on commit f067c4e

Please sign in to comment.