Skip to content

Commit 7aa4d23

Browse files
committed
Add code-editor-idle-extension
1 parent 95a99f7 commit 7aa4d23

File tree

13 files changed

+586
-0
lines changed

13 files changed

+586
-0
lines changed

patched-vscode/build/gulpfile.extensions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const compilations = [
6161
'extensions/search-result/tsconfig.json',
6262
'extensions/simple-browser/tsconfig.json',
6363
'extensions/sagemaker-extension/tsconfig.json',
64+
'extensions/code-editor-idle-extension/tsconfig.json',
6465
'extensions/tunnel-forwarding/tsconfig.json',
6566
'extensions/typescript-language-features/test-workspace/tsconfig.json',
6667
'extensions/typescript-language-features/web/tsconfig.json',

patched-vscode/build/npm/dirs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const dirs = [
4040
'extensions/php-language-features',
4141
'extensions/references-view',
4242
'extensions/sagemaker-extension',
43+
'extensions/code-editor-idle-extension',
4344
'extensions/search-result',
4445
'extensions/simple-browser',
4546
'extensions/tunnel-forwarding',
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.vscode/**
2+
.vscode-test/**
3+
out/test/**
4+
out/**
5+
test/**
6+
src/**
7+
tsconfig.json
8+
out/test/**
9+
out/**
10+
cgmanifest.json
11+
yarn.lock
12+
preview-src/**
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Code Editor Idle Extension
2+
3+
The Code Editor Idle Extension monitors user activity and records the last active timestamp (in UTC) to a local file. User activity includes file changes, cursor movements, and terminal activity.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright Amazon.com Inc. or its affiliates. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
//@ts-check
7+
8+
'use strict';
9+
10+
const withBrowserDefaults = require('../shared.webpack.config').browser;
11+
12+
module.exports = withBrowserDefaults({
13+
context: __dirname,
14+
entry: {
15+
extension: './src/extension.ts'
16+
},
17+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright Amazon.com Inc. or its affiliates. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
//@ts-check
7+
8+
'use strict';
9+
10+
const withDefaults = require('../shared.webpack.config');
11+
12+
module.exports = withDefaults({
13+
context: __dirname,
14+
resolve: {
15+
mainFields: ['module', 'main']
16+
},
17+
entry: {
18+
extension: './src/extension.ts',
19+
}
20+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "code-editor-idle-extension",
3+
"displayName": "Code Editor Idle Extension",
4+
"description": "Expose an API called /idle that returns latest activity timestamp",
5+
"extensionKind": [
6+
"workspace"
7+
],
8+
"version": "1.0.0",
9+
"publisher": "sagemaker",
10+
"license": "MIT",
11+
"engines": {
12+
"vscode": "^1.70.0"
13+
},
14+
"main": "./out/extension",
15+
"categories": [
16+
"Other"
17+
],
18+
"activationEvents": [
19+
"*"
20+
],
21+
"capabilities": {
22+
"virtualWorkspaces": true,
23+
"untrustedWorkspaces": {
24+
"supported": true
25+
}
26+
},
27+
"contributes": {
28+
"configuration": {
29+
"type": "object",
30+
"title": "Code Editor Idle Extension",
31+
"properties": {}
32+
},
33+
"commands": []
34+
},
35+
"scripts": {
36+
"compile": "gulp compile-extension:code-editor-idle-extension",
37+
"watch": "npm run build-preview && gulp watch-extension:code-editor-idle-extension",
38+
"vscode:prepublish": "npm run build-ext",
39+
"build-ext": "node ../../node_modules/gulp/bin/gulp.js --gulpfile ../../build/gulpfile.extensions.js compile-extension:code-editor-idle-extension ./tsconfig.json"
40+
},
41+
"dependencies": {},
42+
"repository": {}
43+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import * as vscode from "vscode";
2+
import * as fs from "fs";
3+
import * as path from "path";
4+
5+
let idleFilePath: string
6+
let terminalActivityInterval: NodeJS.Timeout | undefined
7+
const CHECK_INTERVAL = 60000; // 60 seconds interval
8+
9+
export function activate(context: vscode.ExtensionContext) {
10+
initializeIdleFilePath();
11+
registerEventListeners(context);
12+
startMonitoringTerminalActivity();
13+
}
14+
15+
export function deactivate() {
16+
if(terminalActivityInterval) {
17+
clearInterval(terminalActivityInterval)
18+
}
19+
}
20+
21+
/**
22+
* Initializes the file path where the idle timestamp will be stored.
23+
* It sets the path to a hidden file in the user's home directory.
24+
*/
25+
function initializeIdleFilePath() {
26+
const homeDirectory = process.env.HOME || process.env.USERPROFILE;
27+
if (!homeDirectory) {
28+
console.log("Unable to determine the home directory.");
29+
return;
30+
}
31+
idleFilePath = path.join(homeDirectory, ".code-editor-last-active-timestamp");
32+
}
33+
34+
/**
35+
* Registers event listeners to monitor user activity within the VSCode editor.
36+
* It listens to document changes, editor focus changes, text selection changes, and terminal events.
37+
* @param context - The context in which the extension is running.
38+
*/
39+
function registerEventListeners(context: vscode.ExtensionContext) {
40+
context.subscriptions.push(
41+
vscode.workspace.onDidChangeTextDocument((_) => {
42+
updateLastActivityTimestamp();
43+
}),
44+
vscode.window.onDidChangeActiveTextEditor((_) => {
45+
updateLastActivityTimestamp();
46+
}),
47+
vscode.window.onDidChangeTextEditorSelection((_) => {
48+
updateLastActivityTimestamp();
49+
}),
50+
vscode.window.onDidOpenTerminal((_) => {
51+
updateLastActivityTimestamp();
52+
}),
53+
vscode.window.onDidCloseTerminal((_) => {
54+
updateLastActivityTimestamp();
55+
})
56+
);
57+
}
58+
59+
/**
60+
* Starts monitoring terminal activity by setting an interval to check for activity in the /dev/pts directory.
61+
*/
62+
const startMonitoringTerminalActivity = () => {
63+
terminalActivityInterval = setInterval(checkTerminalActivity, CHECK_INTERVAL);
64+
};
65+
66+
67+
/**
68+
* Checks for terminal activity by reading the /dev/pts directory and comparing modification times of the files.
69+
* If activity is detected, it updates the last activity timestamp.
70+
*/
71+
const checkTerminalActivity = () => {
72+
fs.readdir("/dev/pts", (err, files) => {
73+
if (err) {
74+
console.error("Error reading /dev/pts directory:", err);
75+
return;
76+
}
77+
78+
const now = Date.now();
79+
const activityDetected = files.some((file) => {
80+
const filePath = path.join("/dev/pts", file);
81+
try {
82+
const stats = fs.statSync(filePath);
83+
const mtime = new Date(stats.mtime).getTime();
84+
return now - mtime < CHECK_INTERVAL;
85+
} catch (error) {
86+
console.error("Error reading file stats:", error);
87+
return false;
88+
}
89+
});
90+
91+
if (activityDetected) {
92+
updateLastActivityTimestamp();
93+
}
94+
});
95+
};
96+
97+
/**
98+
* Updates the last activity timestamp by writing the current timestamp to the idle file and updating the status bar.
99+
*/
100+
function updateLastActivityTimestamp() {
101+
const timestamp = new Date().toISOString();
102+
fs.writeFileSync(idleFilePath, timestamp);
103+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "./out"
5+
},
6+
"include": [
7+
"../code-editor-idle-extension/src/**/*",
8+
"../../src/vscode-dts/vscode.d.ts"
9+
]
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+

0 commit comments

Comments
 (0)