Skip to content

Commit

Permalink
Add tests for file paths in dvc.yamls
Browse files Browse the repository at this point in the history
  • Loading branch information
wolmir committed Oct 3, 2022
1 parent acdb4fe commit 49172bd
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 41 deletions.
37 changes: 2 additions & 35 deletions languageServer/src/LanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ import {
Location,
Position,
Range,
DocumentSymbol,
TextDocumentItem
DocumentSymbol
} from 'vscode-languageserver/node'
import { TextDocument } from 'vscode-languageserver-textdocument'
import { URI } from 'vscode-uri'
import { TextDocumentWrapper } from './TextDocumentWrapper'

export class LanguageServer {
private documentsKnownToEditor!: TextDocuments<TextDocument>
private documentsFromDvcClient: TextDocumentWrapper[] = []

public listen(connection: _Connection) {
this.documentsKnownToEditor = new TextDocuments(TextDocument)
Expand All @@ -34,24 +32,6 @@ export class LanguageServer {
return this.onDefinition(params)
})

connection.onRequest(
'initialTextDocuments',
(params: { textDocuments: TextDocumentItem[] }) => {
this.documentsFromDvcClient = params.textDocuments.map(
({ uri, languageId, version, text: content }) => {
const textDocument = TextDocument.create(
uri,
languageId,
version,
content
)

return this.wrap(textDocument)
}
)
}
)

this.documentsKnownToEditor.listen(connection)

connection.listen()
Expand All @@ -61,16 +41,6 @@ export class LanguageServer {
const openDocuments = this.documentsKnownToEditor.all()
const acc: TextDocumentWrapper[] = openDocuments.map(doc => this.wrap(doc))

for (const textDocument of this.documentsFromDvcClient) {
const userAlreadyOpenedIt = this.documentsKnownToEditor.get(
textDocument.uri
)

if (!userAlreadyOpenedIt) {
acc.push(textDocument)
}
}

return acc
}

Expand All @@ -81,10 +51,7 @@ export class LanguageServer {
const doc = this.documentsKnownToEditor.get(uri)

if (!doc) {
const alternative = this.documentsFromDvcClient.find(
txtDoc => txtDoc.uri === uri
)
return alternative ?? null
return null
}

return this.wrap(doc)
Expand Down
39 changes: 34 additions & 5 deletions languageServer/src/languageHelpers/yamlHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ export class YamlHelper extends BaseLanguageHelper<Document> {
) {
const nodeValue = `${node.value}`

let symbolKind: SymbolKind = SymbolKind.String

if (/\.[A-Za-z]+$/.test(nodeValue)) {
symbolKind = SymbolKind.File
}
const symbolKind: SymbolKind = SymbolKind.String

const children: DocumentSymbol[] = []

Expand All @@ -107,6 +103,9 @@ export class YamlHelper extends BaseLanguageHelper<Document> {
children.push(...propertyPathSymbols)
}

const filePathSymbols = this.extractFilePathSymbols(nodeValue, nodeStart)
children.push(...filePathSymbols)

const symbolsSoFar: DocumentSymbol[] = [
DocumentSymbol.create(
nodeValue,
Expand All @@ -121,6 +120,36 @@ export class YamlHelper extends BaseLanguageHelper<Document> {
return symbolsSoFar
}

private extractFilePathSymbols(text: string, startIndex: number) {
const symbols: DocumentSymbol[] = []
const patternMatches = text.matchAll(RegExes.filePaths)

for (const match of patternMatches) {
const matchIndex = match.index ?? 0

symbols.push(this.getFilePathSymbol(match[0], startIndex + matchIndex))
}

return symbols
}

private getFilePathSymbol(path: string, startIndex: number) {
const pathStringLength = path.length
const endIndex = startIndex + pathStringLength
const symbolStart = this.positionAt(startIndex)
const symbolEnd = this.positionAt(endIndex)
const symbolRange = Range.create(symbolStart, symbolEnd)
const symbolSelectionRange = symbolRange

return DocumentSymbol.create(
path,
path,
SymbolKind.File,
symbolRange,
symbolSelectionRange
)
}

private extractPropertyPathSymbolsFrom(text: string, startIndex: number) {
const symbols: DocumentSymbol[] = []
const pathLikeSegments = text.matchAll(RegExes.propertyPathLike)
Expand Down
38 changes: 37 additions & 1 deletion languageServer/src/test/definitions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Position, Range } from 'vscode-languageserver/node'
import { foreach_dvc_yaml, params_dvc_yaml } from './fixtures/examples/valid'
import { TextDocument } from 'vscode-languageserver-textdocument'
import {
file_path_dvc_yaml,
foreach_dvc_yaml,
params_dvc_yaml
} from './fixtures/examples/valid'
import { params } from './fixtures/params'
import { requestDefinitions } from './utils/requestDefinitions'
import { openTheseFilesAndNotifyServer } from './utils/openTheseFilesAndNotifyServer'
Expand Down Expand Up @@ -31,6 +36,19 @@ describe('textDocument/definitions', () => {
expect(response).toBeNull()
})

it('should not provide definitions for files that were not synchronized', async () => {
const fakeDocument = TextDocument.create(
'fakeUri://fakeson/dvc.yaml',
'yaml',
0,
'mock content'
)

const response = await requestDefinitions(fakeDocument, 'mock')

expect(response).toBeNull()
})

it('should be able to point out the symbol under the cursor when needed', async () => {
const [dvcYaml] = await openTheseFilesAndNotifyServer([
{
Expand Down Expand Up @@ -69,4 +87,22 @@ describe('textDocument/definitions', () => {
uri: 'file:///dvc.yaml'
})
})

it('should return the file location if the symbol is a file path', async () => {
const [dvcYaml] = await openTheseFilesAndNotifyServer([
{
languageId: 'yaml',
mockContents: file_path_dvc_yaml,
mockPath: 'dvc.yaml'
},
{
languageId: 'json',
mockContents: '',
mockPath: 'params.json'
}
])
const response = await requestDefinitions(dvcYaml, 'params.json')

expect(response).toBeTruthy()
})
})
15 changes: 15 additions & 0 deletions languageServer/src/test/fixtures/examples/valid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ stages:
persist: false
`

export const minimal_dvc_yaml = `
stages:
stage1:
Expand All @@ -68,6 +69,20 @@ stages:
- echo world
`

export const file_path_dvc_yaml = `
stages:
stage1:
cmd: echo foo
params:
- params.json
stage2:
cmd:
- echo hello
- echo world
`

export const outs_dvc_yaml = `
stages:
copy_multiple:
Expand Down

0 comments on commit 49172bd

Please sign in to comment.