1+ /*---------------------------------------------------------
2+ * Copyright (C) Microsoft Corporation. All rights reserved.
3+ *--------------------------------------------------------*/
4+
5+ 'use strict' ;
6+
7+ import vscode = require( 'vscode' ) ;
8+ import cp = require( 'child_process' ) ;
9+ import { dirname , basename } from 'path' ;
10+ import * as proxy from './jediProxy' ;
11+ import * as fs from 'fs' ;
12+
13+ const mappedTypes = { } ;
14+ mappedTypes [ "module" ] = vscode . CompletionItemKind . Module ;
15+ mappedTypes [ "class" ] = vscode . CompletionItemKind . Class ;
16+ mappedTypes [ "instance" ] = vscode . CompletionItemKind . Variable ;
17+ mappedTypes [ "function" ] = vscode . CompletionItemKind . Function ;
18+ mappedTypes [ "funcdef" ] = vscode . CompletionItemKind . Function ;
19+ mappedTypes [ "property" ] = vscode . CompletionItemKind . Property ;
20+ mappedTypes [ "import" ] = vscode . CompletionItemKind . Module ;
21+ mappedTypes [ "keyword" ] = vscode . CompletionItemKind . Keyword ;
22+ mappedTypes [ "builtin" ] = vscode . CompletionItemKind . Keyword ;
23+ mappedTypes [ "statement" ] = vscode . CompletionItemKind . Value ;
24+ mappedTypes [ "value" ] = vscode . CompletionItemKind . Value ;
25+ mappedTypes [ "variable" ] = vscode . CompletionItemKind . Variable ;
26+ mappedTypes [ "param" ] = vscode . CompletionItemKind . Variable ;
27+ mappedTypes [ "constant" ] = vscode . CompletionItemKind . Variable ;
28+
29+ export class PythonCompletionItemProvider implements vscode . CompletionItemProvider {
30+ public constructor ( rootDir : string ) {
31+ proxy . initialize ( rootDir ) ;
32+ }
33+
34+ private gocodeConfigurationComplete = false ;
35+
36+ public provideCompletionItems ( document : vscode . TextDocument , position : vscode . Position , token : vscode . CancellationToken ) : Thenable < vscode . CompletionItem [ ] > {
37+ return new Promise < vscode . CompletionItem [ ] > ( ( resolve , reject ) => {
38+ var filename = document . fileName ;
39+ if ( document . lineAt ( position . line ) . text . match ( / ^ \s * \/ \/ / ) ) {
40+ return resolve ( [ ] ) ;
41+ }
42+ if ( position . character <= 0 ) {
43+ return resolve ( [ ] ) ;
44+ }
45+ //if current character is '(', then ignore it
46+ var txt = document . getText ( new vscode . Range ( new vscode . Position ( position . line , position . character - 1 ) , position ) ) ;
47+ var type = proxy . CommandType . Completions ;
48+ var columnIndex = position . character ;
49+ if ( txt === "(" ) {
50+ //return resolve([]);
51+ //type = proxy.CommandType.Arguments;
52+ //columnIndex = columnIndex -1;
53+ }
54+ var source = document . getText ( ) ; //fs.realpathSync(filename).toString();
55+ var cmd : proxy . ICommand = {
56+ id : new Date ( ) . getTime ( ) . toString ( ) ,
57+ command : type ,
58+ fileName : filename ,
59+ columnIndex : columnIndex ,
60+ lineIndex : position . line ,
61+ reject : onRejected ,
62+ resolve : onResolved ,
63+ source : source
64+ } ;
65+
66+ console . log ( cmd . lineIndex . toString ( ) + ":" + cmd . columnIndex . toString ( ) ) ;
67+
68+ function onRejected ( error ) {
69+ if ( token . isCancellationRequested ) {
70+ return resolve ( [ ] ) ;
71+ }
72+ var y = "" ;
73+ }
74+ function onResolved ( data : proxy . ICompletionResult ) {
75+ if ( token . isCancellationRequested ) {
76+ return resolve ( [ ] ) ;
77+ }
78+ if ( data && data . items . length > 0 ) {
79+ var items = data . items . map ( item => {
80+ var completionItem = new vscode . CompletionItem ( item . text ) ;
81+ completionItem . documentation = item . description ;
82+ if ( mappedTypes [ item . type ] ) {
83+ completionItem . kind = mappedTypes [ item . type ] ;
84+ }
85+ else {
86+ console . error ( "Unknown type = " + item . type ) ;
87+ }
88+
89+ return completionItem ;
90+ //completionItem.kind
91+ //completionItem.kind = vscode.CompletionItemKind.Class
92+ } ) ;
93+ resolve ( items ) ;
94+ }
95+ else {
96+ resolve ( [ ] ) ;
97+ }
98+ }
99+ proxy . sendCommand ( cmd ) . then ( onResolved , onRejected ) ;
100+ } ) ;
101+ }
102+ }
0 commit comments