Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 600535a
Showing
10 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules/ | ||
built/ | ||
*.log | ||
*.swp | ||
*.swo | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) [2017] [Quramy] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# ts-graphql-plugin | ||
|
||
TypeScript Language Service Pugin for GraphQL. | ||
|
||
## Features | ||
*T.B.D.* | ||
|
||
## How to install | ||
*T.B.D.* | ||
|
||
## License | ||
This software is released under the MIT License, see LICENSE.txt. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "ts-graphql-plugin", | ||
"version": "0.1.0", | ||
"description": "TypeScript Language Service Plugin for GraphQL", | ||
"main": "built/index.js", | ||
"scripts": { | ||
"compile": "tsc -p .", | ||
"watch": "tsc -w -p .", | ||
"prepublish": "npm run compile" | ||
}, | ||
"author": "Quramy", | ||
"license": "MIT", | ||
"dependencies": { | ||
"graphql": "^0.9.1", | ||
"graphql-language-service-interface": "0.0.4" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^2.2.2", | ||
"@types/node": "^7.0.8" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^2.2.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as ts from 'typescript/lib/tsserverlibrary'; | ||
|
||
import { buildClientSchema } from 'graphql'; | ||
import { CompletionItem, getAutocompleteSuggestions } from 'graphql-language-service-interface'; | ||
|
||
export interface GraphQLLanguageServiceAdapterCreateOptions { | ||
schema?: any; | ||
logger?: (msg: string) => void; | ||
} | ||
|
||
export class GraphQLLanguageServiceAdapter { | ||
|
||
private _schema: any; | ||
private _logger: (msg: string) => void = () => { }; | ||
|
||
constructor( | ||
private _getNode: (fileName: string, position) => ts.Node = () => null, | ||
opt: GraphQLLanguageServiceAdapterCreateOptions = { }, | ||
) { | ||
if (opt.logger) this._logger = opt.logger; | ||
if (opt.schema) this.updateSchema(opt.schema); | ||
} | ||
|
||
updateSchema(schema: { data: any }) { | ||
this._schema = buildClientSchema(schema.data); | ||
} | ||
|
||
getCompletionInfo(delegate: (fileName: string, position: number) => ts.CompletionInfo, fileName: string, position: number, ) { | ||
if (!this._schema) return delegate(fileName, position); | ||
const node = this._getNode(fileName, position); | ||
if (!node || node.kind !== ts.SyntaxKind.NoSubstitutionTemplateLiteral) { | ||
return delegate(fileName, position); | ||
} | ||
const cursor = position - node.getStart(); | ||
const text = node.getText(); | ||
const gqlCompletionItems = getAutocompleteSuggestions(this._schema, text, cursor); | ||
this._logger(JSON.stringify(gqlCompletionItems)); | ||
return translateCompletionItems(gqlCompletionItems); | ||
} | ||
|
||
} | ||
|
||
function translateCompletionItems(items: CompletionItem[]): ts.CompletionInfo { | ||
const result: ts.CompletionInfo = { | ||
isGlobalCompletion: false, | ||
isMemberCompletion: false, | ||
isNewIdentifierLocation: false, | ||
entries: items.map(r => { | ||
const kind = r.kind ? r.kind + '' : 'unknown'; | ||
return { | ||
name: r.label, | ||
kindModifiers: 'declare', | ||
kind, | ||
sortText: '0', | ||
}; | ||
}), | ||
}; | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as ts from 'typescript/lib/tsserverlibrary'; | ||
import { GraphQLLanguageServiceAdapter } from "./graphql-language-service-adapter"; | ||
import { LanguageServiceProxyBuilder } from "./language-service-proxy-builder"; | ||
import { SchamaJsonManager } from "./schema-json-manager"; | ||
|
||
function findNode(sourceFile: ts.SourceFile, position: number): ts.Node | undefined { | ||
function find(node: ts.Node): ts.Node|undefined { | ||
if (position >= node.getStart() && position < node.getEnd()) { | ||
return ts.forEachChild(node, find) || node; | ||
} | ||
} | ||
return find(sourceFile); | ||
} | ||
|
||
function create(info: ts.server.PluginCreateInfo): ts.LanguageService { | ||
const getNode = (fileName: string, position: number) => findNode(info.languageService.getProgram().getSourceFile(fileName), position); | ||
const logger = (msg: string) => info.project.projectService.logger.info(msg); | ||
const program = info.languageService.getProgram(); | ||
|
||
const schemaManager = new SchamaJsonManager(info); | ||
const schema = schemaManager.getSchema(); | ||
const adapter = new GraphQLLanguageServiceAdapter(getNode, { schema, logger }); | ||
|
||
const proxy = new LanguageServiceProxyBuilder(info) | ||
.wrap("getCompletionsAtPosition", delegate => adapter.getCompletionInfo.bind(adapter, delegate)) | ||
.build() | ||
; | ||
|
||
schemaManager.registerOnChange(adapter.updateSchema.bind(adapter)); | ||
schemaManager.startWatch(); | ||
|
||
return proxy; | ||
} | ||
|
||
const moduleFactory: ts.server.PluginModuleFactory = function(mod: { typescript: typeof ts }) { | ||
return { create }; | ||
}; | ||
|
||
export = moduleFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as ts from 'typescript/lib/tsserverlibrary'; | ||
|
||
export interface LanguageServiceMethodWrapper<K extends keyof ts.LanguageService> { | ||
(delegate: ts.LanguageService[K], info?: ts.server.PluginCreateInfo): ts.LanguageService[K]; | ||
} | ||
|
||
export class LanguageServiceProxyBuilder { | ||
|
||
private _wrappers = []; | ||
|
||
constructor(private _info: ts.server.PluginCreateInfo) { } | ||
|
||
wrap<K extends keyof ts.LanguageService>(name: K, wrapper: LanguageServiceMethodWrapper<K>) { | ||
this._wrappers.push({ name, wrapper }); | ||
return this; | ||
} | ||
|
||
build() { | ||
const ret = this._info.languageService; | ||
this._wrappers.forEach(({ name, wrapper }) => { | ||
ret[name] = wrapper(this._info.languageService[name], this._info); | ||
}); | ||
return ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as ts from 'typescript/lib/tsserverlibrary'; | ||
|
||
export class SchamaJsonManager { | ||
private _schemaPath: string; | ||
private _watcher: ts.FileWatcher; | ||
private _onChanges: ((schema: any) => void)[] = []; | ||
|
||
constructor(private _info: ts.server.PluginCreateInfo) { | ||
this._schemaPath = this._info.config.schema; | ||
} | ||
|
||
private _log(msg: string) { | ||
this._info.project.projectService.logger.info(msg); | ||
} | ||
|
||
getSchema() { | ||
if (!this._schemaPath || typeof this._schemaPath !== 'string') return; | ||
try { | ||
const isExists = this._info.languageServiceHost.fileExists(this._schemaPath); | ||
if (!isExists) return; | ||
return JSON.parse(this._info.languageServiceHost.readFile(this._schemaPath, 'utf-8')); | ||
} catch (e) { | ||
this._log('Fail to read schema file...'); | ||
this._log(e.message); | ||
return; | ||
} | ||
} | ||
|
||
registerOnChange(cb: (schema: any) => void) { | ||
this._onChanges.push(cb); | ||
return () => { | ||
this._onChanges.filter(x => x !== cb); | ||
}; | ||
} | ||
|
||
startWatch(interval: number = 100) { | ||
try { | ||
this._watcher = this._info.serverHost.watchFile(this._schemaPath, () => { | ||
this._log("Change schema file."); | ||
if (this._onChanges.length) { | ||
const schema = this.getSchema(); | ||
if (schema) this._onChanges.forEach(cb => cb(schema)); | ||
} | ||
}, interval); | ||
} catch (e) { | ||
this._log('Fail to read schema file...'); | ||
this._log(e.message); | ||
return; | ||
} | ||
} | ||
|
||
closeWatch() { | ||
if (this._watcher) this._watcher.close(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
declare module 'graphql-language-service-interface' { | ||
export interface State { | ||
level: number; | ||
levels?: number[]; | ||
prevState: State; | ||
rule: any; // tmp | ||
kind: string; | ||
name: string; | ||
type: string; | ||
step: number; | ||
needsSeperator: boolean; | ||
needsAdvance?: boolean; | ||
indentLevel?: number; | ||
} | ||
|
||
export interface ContextToken { | ||
start: number; | ||
end: number; | ||
string: string; | ||
state: State; | ||
style: string; | ||
} | ||
|
||
export interface CompletionItem { | ||
label: string; | ||
kind?: number; | ||
detail?: string; | ||
documentation?: string; | ||
// GraphQL Deprecation information | ||
isDeprecated?: string; | ||
deprecationReason?: string; | ||
} | ||
|
||
export function getTokenAtPosition(queryText: string, cursor: number): ContextToken; | ||
|
||
export function getAutocompleteSuggestions(schema: any, queryText: string, cursor: number, contextToken?: ContextToken): CompletionItem[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5", | ||
"sourceMap": false, | ||
"outDir": "built", | ||
"rootDir": "src", | ||
"lib": ["es2015"] | ||
}, | ||
"exclude": ["built", "node_modules"] | ||
} |