Skip to content

Commit

Permalink
Add the completion Support
Browse files Browse the repository at this point in the history
  • Loading branch information
nadeeshaan committed Sep 18, 2018
1 parent cbd29b5 commit 6881826
Show file tree
Hide file tree
Showing 16 changed files with 1,087 additions and 6 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2018 Nadeeshaan Gunasinghe

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
8 changes: 8 additions & 0 deletions client/.vscodeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.vscode/**
typings/**
out/test/**
test/**
src/**
**/*.map
.gitignore
tsconfig.json
53 changes: 53 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "swagger-language-sever-extension",
"description": "VSCode Client extension for Swagger",
"author": "Nadeeshaan",
"license": "MIT",
"version": "0.0.1",
"publisher": "nadeeshaan",
"engines": {
"vscode": "^1.26.0"
},
"categories": [
"Other"
],
"activationEvents": [
"onLanguage:yaml"
],
"main": "./out/extension",
"contributes": {
"configuration": {
"type": "object",
"title": "Example configuration",
"properties": {
"languageServerExample.maxNumberOfProblems": {
"scope": "resource",
"type": "number",
"default": 100,
"description": "Controls the maximum number of problems produced by the server."
}
}
}
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install",
"build:grammar": "tsc -p syntaxes/build && node syntaxes/build/build.js",
"test": "npm run compile && node ./node_modules/vscode/bin/test",
"clean": "rimraf server-build target",
"package": "npm run compile && vsce package"
},
"devDependencies": {
"vscode": "^1.1.5",
"@types/node": "^8.10.25",
"tslint": "^5.8.0",
"typescript": "^2.6.1",
"vsce": "^1.36.2"
},
"dependencies": {
"vscode": "^1.1.21",
"vscode-languageclient": "^4.1.4"
}
}
43 changes: 43 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';

import * as path from 'path';

import { workspace, Disposable, ExtensionContext } from 'vscode';
import { LanguageClient, LanguageClientOptions, SettingMonitor, ServerOptions, TransportKind } from 'vscode-languageclient';

const main: string = 'StdioLauncher';

export function activate(context: ExtensionContext) {

const { JAVA_HOME } = process.env;
console.log(`Using java from JAVA_HOME: ${JAVA_HOME}`);
let excecutable : string = path.join(JAVA_HOME, 'bin', 'java');

let jarPath = path.join(__dirname, '..', 'launcher', 'ls-launcher.jar');
const args: string[] = ['-cp', jarPath];
args.push('-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005,quiet=y');
console.log(jarPath);

let serverOptions: ServerOptions = {
command: excecutable,
args: [...args, main],
options: {}
};

// Options to control the language client
let clientOptions: LanguageClientOptions = {
// Register the server for plain text documents
documentSelector: [{scheme: 'file', language: 'yaml'}]
};

// Create the language client and start the client.
let disposable = new LanguageClient('languageServerExample', 'Language Server Example', serverOptions, clientOptions).start();

// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(disposable);
}
14 changes: 14 additions & 0 deletions client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "out",
"lib": [ "es2016" ],
"sourceMap": true
},
"exclude": [
"node_modules",
"server"
]
}
31 changes: 31 additions & 0 deletions langserver-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
~ Copyright (c) 2018, Nadeeshaan Gunasinghe
~
~ WSO2 Inc. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>swagger-language-server</artifactId>
<groupId>org.swagger.ls</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>langserver-core</artifactId>
<packaging>jar</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2018, Nadeeshaan Gunasinghe
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.swagger.langserver;

import java.nio.file.Path;

/**
* Document Manager is responsible for maintaining the content of the documents.
*/
public interface DocumentManager {
/**
* Checks whether the given file is open in workspace.
*
* @param filePath Path of the file
* @return True if the given file is open
*/
boolean isFileOpen(Path filePath);

/**
* Opens the given file in document manager.
*
* @param filePath Path of the file
* @param content Content of the file
*/
void openFile(Path filePath, String content);

/**
* Updates given file in document manager with new content.
*
* @param filePath Path of the file
* @param updatedContent New content of the file
*/
void updateFile(Path filePath, String updatedContent);

/**
* Close the given file in document manager.
*
* @param filePath Path of the file
*/
void closeFile(Path filePath);

/**
* Gets uptodate content of the file.
*
* @param filePath Path of the file
* @return Content of the file
*/
String getFileContent(Path filePath);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2018, Nadeeshaan Gunasinghe
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.swagger.langserver;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Implementation of DocumentManager Interface.
*/
public class DocumentManagerImpl implements DocumentManager {

private static final Logger LOGGER = LoggerFactory.getLogger(DocumentManagerImpl.class);

private ConcurrentHashMap<Path, String> documents;

private DocumentManagerImpl() {
this.documents = new ConcurrentHashMap<>();
}

private static class InnerSingleton {
private static final DocumentManagerImpl INSTANCE = new DocumentManagerImpl();
}

public static DocumentManagerImpl getInstance() {
return InnerSingleton.INSTANCE;
}

/**
* Checks whether the given file is open in workspace.
*
* @param filePath Path of the file
* @return Returns the list of opened document paths
*/
@Override
public boolean isFileOpen(Path filePath) {
return getPathEntry(filePath) != null;
}

/**
* Opens the given file in document manager.
*
* @param filePath Path of the file
* @param content Content of the file
*/
@Override
public void openFile(Path filePath, String content) {
if (isFileOpen(filePath)) {
LOGGER.warn("File is Already opened");
return;
}
this.documents.put(filePath, content);
}

/**
* Updates given file in document manager with new content.
*
* @param filePath Path of the file
* @param updatedContent New content of the file
*/
@Override
public void updateFile(Path filePath, String updatedContent) {
Path opened = getPathEntry(filePath);
if (opened == null) {
LOGGER.error("Cannot find the file to update: " + filePath.toString());
return;
}

this.documents.put(opened, updatedContent);
}

/**
* Close the given file in document manager.
*
* @param filePath Path of the file
*/
@Override
public void closeFile(Path filePath) {
Path opened = getPathEntry(filePath);
if (opened == null) {
LOGGER.warn("Cannot find open file [" + filePath.toString() + "] to close");
return;
}

this.documents.remove(opened);
}

/**
* Gets uptodate content of the file.
*
* @param filePath Path of the file
* @return Content of the file
*/
@Override
public String getFileContent(Path filePath) {
if (!isFileOpen(filePath)) {
return null;
}
return documents.get(filePath);
}

// Private methods

/**
* Get the path entry for the given file path.
*
* @param filePath File Path to se
* @return {@link Path} Path Entry
*/
private Path getPathEntry(Path filePath) {
return this.documents.entrySet().stream().filter(entry -> {
try {
return Files.isSameFile(entry.getKey(), filePath);
} catch (IOException e) {
LOGGER.error("Error locating File: " + e.getMessage());
return false;
}
}).map(Map.Entry::getKey).findFirst().orElse(null);
}
}
Loading

0 comments on commit 6881826

Please sign in to comment.