|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +'use strict'; |
| 4 | +import '../common/extensions'; |
| 5 | + |
| 6 | +import { inject, injectable } from 'inversify'; |
| 7 | +import * as path from 'path'; |
| 8 | +import { TextDocument } from 'vscode'; |
| 9 | + |
| 10 | +import { sendTelemetryEvent } from '.'; |
| 11 | +import { sleep } from '../../test/core'; |
| 12 | +import { IDocumentManager } from '../common/application/types'; |
| 13 | +import { IHistoryProvider } from '../datascience/types'; |
| 14 | +import { ICodeExecutionManager } from '../terminals/types'; |
| 15 | +import { EventName, KnownImports } from './constants'; |
| 16 | +import { IImportTracker } from './types'; |
| 17 | + |
| 18 | +const ImportRegEx = /^(?!['"#]).*from\s+([a-zA-Z0-9_\.]+)\s+import.*(?!['"])|^(?!['"#]).*import\s+([a-zA-Z0-9_\., ]+).*(?!['"])/; |
| 19 | +const MAX_DOCUMENT_LINES = 1000; |
| 20 | + |
| 21 | +@injectable() |
| 22 | +export class ImportTracker implements IImportTracker { |
| 23 | + |
| 24 | + private knownImportsMatch: RegExp; |
| 25 | + private sentMatches: Set<string> = new Set<string>(); |
| 26 | + |
| 27 | + constructor( |
| 28 | + @inject(IDocumentManager) private documentManager: IDocumentManager, |
| 29 | + @inject(IHistoryProvider) private historyProvider: IHistoryProvider, |
| 30 | + @inject(ICodeExecutionManager) private executionManager: ICodeExecutionManager |
| 31 | + ) { |
| 32 | + // Construct a regex that will match known imports |
| 33 | + let matchString = ''; |
| 34 | + Object.keys(KnownImports).forEach(k => { |
| 35 | + const val = KnownImports[k]; |
| 36 | + if (typeof val === 'string') { |
| 37 | + matchString += `${val}|`; |
| 38 | + } |
| 39 | + }); |
| 40 | + this.knownImportsMatch = new RegExp(matchString.slice(0, matchString.length - 1), 'g'); |
| 41 | + |
| 42 | + // Sign up for document open/save events so we can track known imports |
| 43 | + this.documentManager.onDidOpenTextDocument((t) => this.onOpenedOrSavedDocument(t)); |
| 44 | + this.documentManager.onDidSaveTextDocument((t) => this.onOpenedOrSavedDocument(t)); |
| 45 | + |
| 46 | + // Sign up for history execution events (user can input code here too) |
| 47 | + this.historyProvider.onExecutedCode(c => this.onExecutedCode(c)); |
| 48 | + |
| 49 | + // Sign up for terminal execution events (user can send code to the terminal) |
| 50 | + // However we won't get any text typed directly into the terminal. Not part of the VS code API |
| 51 | + // Could potentially hook stdin? Not sure that's possible. |
| 52 | + this.executionManager.onExecutedCode(c => this.onExecutedCode(c)); |
| 53 | + } |
| 54 | + |
| 55 | + public async activate() : Promise<void> { |
| 56 | + // Act like all of our open documents just opened. Don't do this now though. We don't want |
| 57 | + // to hold up the activate. |
| 58 | + await sleep(1000); |
| 59 | + this.documentManager.textDocuments.forEach(d => this.onOpenedOrSavedDocument(d)); |
| 60 | + } |
| 61 | + |
| 62 | + private onOpenedOrSavedDocument(document: TextDocument) { |
| 63 | + // Make sure this is a python file. |
| 64 | + if (path.extname(document.fileName) === '.py') |
| 65 | + { |
| 66 | + // Parse the contents of the document, looking for import matches on each line |
| 67 | + const lines = document.getText().splitLines({ trim: true, removeEmptyEntries: true }); |
| 68 | + this.lookForImports(lines.slice(0, Math.min(lines.length, MAX_DOCUMENT_LINES)), EventName.KNOWN_IMPORT_FROM_FILE); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private onExecutedCode(code: string) { |
| 73 | + const lines = code.splitLines({ trim: true, removeEmptyEntries: true }); |
| 74 | + this.lookForImports(lines, EventName.KNOWN_IMPORT_FROM_EXECUTION); |
| 75 | + } |
| 76 | + |
| 77 | + private lookForImports(lines: string[], eventName: string) { |
| 78 | + // Use a regex to parse each line, looking for imports |
| 79 | + const matches: Set<string> = new Set<string>(); |
| 80 | + for (const s of lines) { |
| 81 | + const match = ImportRegEx.exec(s); |
| 82 | + if (match && match.length > 2) { |
| 83 | + // Could be a from or a straight import. from is the first entry. |
| 84 | + const actual = match[1] ? match[1] : match[2]; |
| 85 | + |
| 86 | + // See if this matches any known imports |
| 87 | + let knownMatch: RegExpExecArray = this.knownImportsMatch.exec(actual); |
| 88 | + while (knownMatch) { |
| 89 | + knownMatch.forEach(val => { |
| 90 | + // Skip if already sent this telemetry |
| 91 | + if (!this.sentMatches.has(val)) { |
| 92 | + matches.add(val); |
| 93 | + } |
| 94 | + }); |
| 95 | + knownMatch = this.knownImportsMatch.exec(actual); |
| 96 | + } |
| 97 | + |
| 98 | + // Reset search. |
| 99 | + this.knownImportsMatch.lastIndex = 0; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // For each unique match, emit a new telemetry event. |
| 104 | + matches.forEach(s => { |
| 105 | + sendTelemetryEvent( |
| 106 | + eventName === EventName.KNOWN_IMPORT_FROM_FILE ? EventName.KNOWN_IMPORT_FROM_FILE : EventName.KNOWN_IMPORT_FROM_EXECUTION, |
| 107 | + 0, |
| 108 | + { import: s }); |
| 109 | + this.sentMatches.add(s); |
| 110 | + }); |
| 111 | + } |
| 112 | +} |
0 commit comments