From 1a17cf82a2d3fd2c546c1cb651d72885a5c77e68 Mon Sep 17 00:00:00 2001
From: SeeBasTStick <sebastian.stock@hhu.de>
Date: Thu, 28 May 2020 17:26:50 +0200
Subject: [PATCH] save commit

---
 README.md                  |   2 +-
 client/src/extension.ts    |   9 ++-
 server/src/ErrorMatcher.ts | 151 +++++++++++++++++++++++++++++++++++--
 server/src/server.ts       |  19 +++++
 4 files changed, 170 insertions(+), 11 deletions(-)

diff --git a/README.md b/README.md
index ce3e37b..b7b0ce5 100644
--- a/README.md
+++ b/README.md
@@ -30,7 +30,7 @@ Please note that user settings overwrite workspace settings.
 
 
 ## Future plans
-- Linter Support (via Server, is still in development by Mircosoft)
+- Linter Support (via Server, is still in development by Mircosoft https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/)
 - Quickfix support aká Code Completion
 
 
diff --git a/client/src/extension.ts b/client/src/extension.ts
index 3ec716a..f044901 100644
--- a/client/src/extension.ts
+++ b/client/src/extension.ts
@@ -12,7 +12,7 @@ import {
 	ServerOptions,
 	TransportKind
 } from 'vscode-languageclient';
-import * as api from  'vscode';
+import * as vscode from  'vscode';
 
 let client: LanguageClient;
 
@@ -55,12 +55,12 @@ export function activate(context: ExtensionContext) {
 	);
 
 	client.onReady().then(() => {
-		let bla = api.window.createOutputChannel("internal_error")
+		let bla = vscode.window.createOutputChannel("internal_error")
 		client.onNotification("path_error_prob", (message:string) => {
-			api.window.showErrorMessage('a problem occured :' + message)
+			vscode.window.showErrorMessage('a problem occured :' + message)
 		});
 		client.onNotification("parse_error_prob", (message:string) => {
-			api.window.showErrorMessage('a error occured :' + message)
+			vscode.window.showErrorMessage('a error occured :' + message)
 		});
 	});
 
@@ -79,3 +79,4 @@ export function deactivate(): Thenable<void> | undefined {
 	}
 	return client.stop();
 }
+
diff --git a/server/src/ErrorMatcher.ts b/server/src/ErrorMatcher.ts
index c977262..4f6c94d 100644
--- a/server/src/ErrorMatcher.ts
+++ b/server/src/ErrorMatcher.ts
@@ -1,11 +1,103 @@
 import { Diagnostic, DiagnosticSeverity } from 'vscode-languageserver'
-import { TextDocument } from 'vscode-languageserver-textdocument'
+import { TextDocument, Position } from 'vscode-languageserver-textdocument'
 import * as fs from 'fs';
 import * as readline from 'readline'
+import { Uri } from 'vscode';
 
 export class ErrorMatcher {
 
 
+    /**
+	 * Reads the error file, casts the errors to a readable form, sorts them by origin
+	 * @param errorPath the path to the file where the ndjson errors are stored
+	 */
+	async readErrors(errorPath : string) : Promise<Map<Uri, Set<Error | Warning | Information>>> {
+		let result : Map<Uri, Set<Error | Warning | Information>> = new Map()
+
+		var stream = fs.createReadStream(errorPath)
+
+		const rl = readline.createInterface({
+			input: stream,
+			crlfDelay: Infinity
+		});
+
+		type ErrorInfromationWarning = Error | Information | Warning
+		for await (const line of rl) {
+
+				let obj: ErrorInfromationWarning = JSON.parse(line)
+
+				let serveity: DiagnosticSeverity
+				let content: JBody
+				let path : Uri
+
+			
+
+				if (isError(obj)) {
+					path = Uri.parse(obj.error.file)
+				}
+				else {
+					if (isWarning(obj)) {
+						path = Uri.parse(obj.warning.file)
+
+					} else {
+						path = Uri.parse(obj.information.file)
+
+					}
+				}
+
+				if(!result.has(path)){
+					result.set(path, new Set())
+				}
+
+				result.get(path)!.add(obj)
+		}
+
+		return result
+	}
+
+
+	matchErrors(infos : Set<Error | Information | Warning>) : Set<Diagnostic>{
+		let result : Set<Diagnostic>
+		type ErrorInfromationWarning = Error | Information | Warning
+
+		for (let info : ErrorInfromationWarning in infos){
+
+			let serveity : DiagnosticSeverity
+
+			if(isError(info)){
+				serveity = DiagnosticSeverity.Error
+		
+			}else{			
+				if(isWarning(info)){
+					serveity = DiagnosticSeverity.Warning
+				}else{
+				serveity = DiagnosticSeverity.Warning
+			}}
+					
+
+			let diagnostic = {
+				severity: serveity,
+				range: {
+					start:
+					{
+						character: in.start.col,
+						line: content.start.line - 1
+					},
+					end: {
+						character: content.end.col,
+						line: content.end.line - 1
+					}
+				},
+				message: content.message,
+				source: 'prob_prolog',
+				
+			};
+			diagnostics.add(diagnostic)
+		}
+			)
+
+	}
+
 	async matchError(target: TextDocument, errorPath: string): Promise<Set<Diagnostic>> {
 		var diagnostics: Set<Diagnostic> = new Set()
 
@@ -40,6 +132,18 @@ export class ErrorMatcher {
 					}
 				}
 
+				
+
+				if(content.start.line == -1 && content.start.col == -1 && content.end.line == -1 && content.end.col == -1){
+					console.log("Error without position")
+					let targetLine = getFirstOccurence(target).line
+					console.log("new pos is: " + targetLine)
+					content.start = {line : targetLine , col: 0 }
+					content.end = {line : targetLine , col: Number.MAX_VALUE }
+				}
+				
+				
+				
 				diagnostic = {
 					severity: serveity,
 					range: {
@@ -54,7 +158,8 @@ export class ErrorMatcher {
 						}
 					},
 					message: content.message,
-					source: 'prob_prolog'
+					source: 'prob_prolog',
+					
 				};
 				diagnostics.add(diagnostic)
 			}
@@ -79,17 +184,20 @@ export declare type Without<T, U> = {
 	[P in Exclude<keyof T, keyof U>]?: never;
 };
 
-export interface Information {
-	information: JBody
+
+export interface CommonInfo{}
+
+export interface Information extends CommonInfo {
+	information : JBody
 }
 
 
-export interface Error {
+export interface Error extends CommonInfo {
 	error: JBody;
 
 }
 
-export interface Warning {
+export interface Warning extends CommonInfo{
 	warning: JBody
 }
 
@@ -106,3 +214,34 @@ export interface StartOrEnd {
 	col: number;
 }
 
+function getFirstOccurence(textdocument : TextDocument) : Position {
+	let counter : number = textdocument.getText().search(/\S|$/)
+	let pos : Position = textdocument.positionAt(counter)
+	pos.line = pos.line + 1
+	return pos
+}
+
+
+function isError (potential : CommonInfo) : potential is Error{
+	if((potential as Error).error){
+		return true
+	}else{
+		return false
+	}
+}
+
+function isWarning (potential : CommonInfo) : potential is Warning{
+	if((potential as Warning).warning){
+		return true
+	}else{
+		return false
+	}
+}
+
+function isInformation (potential : CommonInfo) : potential is Information{
+	if((potential as Information).information){
+		return true
+	}else{
+		return false
+	}
+}
\ No newline at end of file
diff --git a/server/src/server.ts b/server/src/server.ts
index db045f2..5953b0e 100644
--- a/server/src/server.ts
+++ b/server/src/server.ts
@@ -10,12 +10,19 @@ import {
 	TextDocumentSyncKind,
 	InitializeResult,
 	TextDocumentItem,
+	
 } from 'vscode-languageserver';
 
 import {
 	TextDocument
 } from 'vscode-languageserver-textdocument';
 
+
+import {Proposed} from 'vscode-languageserver-protocol'
+
+
+
+
 import { URI } from 'vscode-uri';
 import * as fs from 'fs';
 import {ErrorMatcher} from './ErrorMatcher';
@@ -27,6 +34,7 @@ import * as path from 'path';
 // Create a connection for the server. The connection uses Node's IPC as a transport.
 // Also include all preview / proposed LSP features.
 let connection = createConnection(ProposedFeatures.all);
+let test = createConnection(ProposedFeatures.all)
 
 // Create a simple text document manager. The text document manager
 // supports full document sync only
@@ -37,6 +45,8 @@ let hasWorkspaceFolderCapability: boolean = false;
 let hasDiagnosticRelatedInformationCapability: boolean = false;
 
 
+
+
 connection.onInitialize((params: InitializeParams) => {
 	let capabilities = params.capabilities;
 
@@ -54,6 +64,9 @@ connection.onInitialize((params: InitializeParams) => {
 		capabilities.textDocument.publishDiagnostics.relatedInformation
 	);
 
+	
+
+	
 	const result: InitializeResult = {
 		capabilities: {
 			textDocumentSync: TextDocumentSyncKind.Full,
@@ -144,6 +157,12 @@ documents.onDidClose(e => {
 	documentSettings.delete(e.document.uri);
 });
 
+
+
+documents.onDidOpen(change => {
+	validateTextDocument(change.document)
+})
+
 documents.onDidSave(change => {
 	validateTextDocument(change.document)
 })
-- 
GitLab