Skip to content

Commit

Permalink
Merge pull request #41 from uqbar-project/validate-all-environment
Browse files Browse the repository at this point in the history
Validate all environment
  • Loading branch information
PalumboN authored Nov 13, 2022
2 parents c25dd6a + d5ae796 commit 7441eb6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
15 changes: 10 additions & 5 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
* ------------------------------------------------------------------------------------------ */

import * as path from 'path'
import { ExtensionContext, workspace } from 'vscode'
import { ExtensionContext, workspace, languages } from 'vscode'
import { LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind } from 'vscode-languageclient/node'
LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient/node'
import { subscribeWollokCommands } from './commands'

let client: LanguageClient
Expand Down Expand Up @@ -47,7 +45,6 @@ export function activate(context: ExtensionContext): void {
// Subscribe Wollok Commands
subscribeWollokCommands(context)


// Create the language client and start the client.
client = new LanguageClient(
'wollok-lsp-ide',
Expand All @@ -56,6 +53,14 @@ export function activate(context: ExtensionContext): void {
clientOptions
)

// Force document changes for first validation
workspace.findFiles('**/*.wlk').then(async uris => {
for (const uri of uris) {
const textDoc = await workspace.openTextDocument(uri)
languages.setTextDocumentLanguage(textDoc, 'wollok')
}
})

// Start the client. This will also launch the server
client.start()
}
Expand Down
48 changes: 23 additions & 25 deletions server/src/linter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import path from 'path'
import { CompletionItem, CompletionItemKind, Connection, Diagnostic, DiagnosticSeverity, InsertTextFormat, Position, TextDocumentPositionParams } from 'vscode-languageserver'
import { TextDocument } from 'vscode-languageserver-textdocument'
import { buildEnvironment, Environment, Node, validate } from 'wollok-ts'
import { Problem } from 'wollok-ts'
import { buildEnvironment, Environment, Node, Problem, validate } from 'wollok-ts'
import { completionsForNode, NodeCompletion } from './autocomplete'
import { reportMessage } from './reporter'
import { TimeMeasurer } from './timeMeasurer'
import { updateDocumentSettings } from './settings'
import { TimeMeasurer } from './timeMeasurer'

// ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════
// INTERNAL FUNCTIONS
Expand All @@ -32,16 +32,17 @@ const createDiagnostic = (textDocument: TextDocument, problem: Problem) => {

// TODO: Refactor and move to utils
const include = (node: Node, { position, textDocument: { uri } }: TextDocumentPositionParams) => {
if (!node.sourceFileName()) return false
if (node.kind === 'Package') {
return uri.includes(node.sourceFileName()!)
}
const startLine = node.sourceMap?.start?.line
const endLine = node.sourceMap?.end?.line
if(node.kind === 'Package'){
return uri === node.sourceFileName()
}
return node.sourceFileName() == uri && startLine && endLine &&
(startLine - 1 <= position.line && position.line <= endLine + 1 ||
startLine - 1 == position.line && position.line == endLine + 1 &&
return uri.includes(node.sourceFileName()!) && startLine && endLine &&
(startLine - 1 <= position.line && position.line <= endLine + 1 ||
startLine - 1 == position.line && position.line == endLine + 1 &&
(node?.sourceMap?.start?.offset || 0) <= position.character && position.character <= endLine
)
)
}

// TODO: Use map instead of forEach
Expand All @@ -62,10 +63,10 @@ const createCompletionItem = (_position: Position) => (base: NodeCompletion): Co
})

function findFirstStableNode(node: Node): Node {
if(!node.problems){
if (!node.problems || node.problems.length === 0) {
return node
}
if(node.parent.kind === 'Environment'){
if (node.parent.kind === 'Environment') {
throw new Error('No stable node found')
}
return findFirstStableNode(node.parent)
Expand All @@ -80,23 +81,22 @@ let environment: Environment
export const validateTextDocument = (connection: Connection) => async (textDocument: TextDocument): Promise<void> => {
await updateDocumentSettings(connection)

const text = textDocument.getText()
const uri = textDocument.uri
const name = path.basename(uri)
const content = textDocument.getText()
try {
const timeMeasurer = new TimeMeasurer()

environment = buildEnvironment([])
timeMeasurer.addTime('build empty environment')

const file: { name: string, content: string } = {
name: textDocument.uri,
content: text,
}
const file: { name: string, content: string } = { name, content }
environment = buildEnvironment([file], environment)
const problems = validate(environment)
timeMeasurer.addTime('build environment for file')

const diagnostics: Diagnostic[] = problems.map(problem => createDiagnostic(textDocument, problem))
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics })
const diagnostics: Diagnostic[] = problems
.filter(problem => problem.node.sourceFileName() == name)
.map(problem => createDiagnostic(textDocument, problem))

connection.sendDiagnostics({ uri, diagnostics })
timeMeasurer.addTime('validation time')

timeMeasurer.finalReport()
Expand All @@ -115,14 +115,12 @@ export const validateTextDocument = (connection: Connection) => async (textDocum
offset: 0,
}, end: {
line: Number.MAX_VALUE,
offset: text.length - 1,
offset: content.length - 1,
},
},
} as unknown as Problem),
],
})
connection.console.error('programa: ' + text)
connection.console.error((e as { message: string}).message)
}
}

Expand Down

0 comments on commit 7441eb6

Please sign in to comment.