Skip to content

Commit 1bc5e29

Browse files
Доработки по замечаниям
1. Добавлен новый класс WorkspaceMapper для обработки путей 2. Убрана установка текущего процесса в _process при отсутствии настоящего 3. Обновлен package.json для использования объекта вместо отдельных путей маппинга 4. Вызов создания маппера помещен в Initialize
1 parent 37c237d commit 1bc5e29

5 files changed

Lines changed: 143 additions & 74 deletions

File tree

src/VSCode.DebugAdapter/DebugeeProcess.cs

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public DebugeeProcess(PathHandlingStrategy pathHandling)
4343
}
4444

4545
public bool HasExited => _process?.HasExited ?? true;
46-
public int ExitCode => _process.ExitCode;
46+
public int ExitCode => _process?.ExitCode ?? 0;
4747

4848
public int DebugPort { get; set; }
4949

@@ -59,9 +59,7 @@ public int ProtocolVersion
5959

6060
public bool WaitOnStart { get; set; }
6161

62-
public string HostWorkspace { get; set; }
63-
64-
public string RemoteWorkspace { get; set; }
62+
public WorkspaceMapper PathsMapper { get; set; }
6563

6664
public void Start()
6765
{
@@ -95,15 +93,15 @@ public void InitAttached()
9593
try
9694
{
9795
_process = Process.GetProcessById(pid);
96+
_process.EnableRaisingEvents = true;
97+
_process.Exited += Process_Exited;
9898
}
9999
catch
100100
{
101-
_process = Process.GetCurrentProcess();
101+
_process = null;
102102
}
103103

104104
_attachMode = true;
105-
_process.EnableRaisingEvents = true;
106-
_process.Exited += Process_Exited;
107105

108106
}
109107

@@ -198,31 +196,6 @@ private void RaiseOutputReceivedEvent(string category, string data)
198196
OutputReceived?.Invoke(this, new DebugeeOutputEventArgs(category, data));
199197
}
200198

201-
private string ConvertRemotePath(string path, string fromPrefix, string toPrefix)
202-
{
203-
if (string.IsNullOrWhiteSpace(path) ||
204-
string.IsNullOrWhiteSpace(fromPrefix) ||
205-
string.IsNullOrWhiteSpace(toPrefix))
206-
return path;
207-
208-
var normalizedPath = path.Replace('\\', '/');
209-
var normalizedFrom = fromPrefix.Replace('\\', '/').TrimEnd('/');
210-
211-
var comparison = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
212-
? StringComparison.OrdinalIgnoreCase
213-
: StringComparison.Ordinal;
214-
215-
if (normalizedPath.StartsWith(toPrefix.Replace('\\', '/'), comparison) ||
216-
!normalizedPath.StartsWith(normalizedFrom, comparison))
217-
return path;
218-
219-
var relativePath = normalizedPath.Substring(normalizedFrom.Length).TrimStart('/');
220-
221-
var result = toPrefix.TrimEnd('/', '\\') + "/" + relativePath;
222-
223-
return result;
224-
}
225-
226199
private void Terminate()
227200
{
228201
if (!_terminated)
@@ -270,6 +243,9 @@ public void HandleDisconnect(bool terminate)
270243

271244
public void Kill()
272245
{
246+
if (_process == null)
247+
return;
248+
273249
_process.Kill();
274250
_process.WaitForExit(1500);
275251
}
@@ -302,7 +278,7 @@ public Breakpoint[] SetBreakpoints(IEnumerable<Breakpoint> breakpoints)
302278

303279
for (int i = 0; i < breakpointsArray.Length; i++)
304280
{
305-
breakpointsArray[i].Source = ConvertRemotePath(breakpointsArray[i].Source, HostWorkspace, RemoteWorkspace);
281+
breakpointsArray[i].Source = PathsMapper.LocalToRemote(breakpointsArray[i].Source);
306282
}
307283

308284
var confirmedBreaks = _debugger.SetMachineBreakpoints(breakpointsArray);
@@ -329,7 +305,7 @@ public StackFrame[] GetStackTrace(int threadId, int firstFrameIdx, int limit)
329305
for (int i = firstFrameIdx; i < limit && i < allFrames.Length; i++)
330306
{
331307
allFrames[i].ThreadId = threadId;
332-
allFrames[i].Source = ConvertRemotePath(allFrames[i].Source, RemoteWorkspace, HostWorkspace);
308+
allFrames[i].Source = PathsMapper.RemoteToLocal(allFrames[i].Source);
333309
result.Add(allFrames[i]);
334310
}
335311

src/VSCode.DebugAdapter/OscriptDebugSession.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ public override void Initialize(Response response, dynamic args)
3737

3838
_debuggee = DebugeeFactory.CreateProcess(AdapterID, PathStrategy);
3939

40+
dynamic pathsMappingObj = GetFromContainer<dynamic>(args, "pathsMapping", null);
41+
42+
if (pathsMappingObj != null)
43+
{
44+
string localPath = GetFromContainer(pathsMappingObj, "localPath", "");
45+
string remotePath = GetFromContainer(pathsMappingObj, "remotePath", "");
46+
47+
_debuggee.PathsMapper = new WorkspaceMapper(localPath, remotePath);
48+
}
49+
else
50+
{
51+
_debuggee.PathsMapper = new WorkspaceMapper("", "");
52+
}
53+
4054
SendResponse(response, new Capabilities
4155
{
4256
supportsConditionalBreakpoints = true,
@@ -139,8 +153,6 @@ public override void Attach(Response response, dynamic arguments)
139153
SubscribeForDebuggeeProcessEvents();
140154

141155
_debuggee.DebugPort = GetFromContainer(arguments, "debugPort", 2801);
142-
_debuggee.HostWorkspace = GetFromContainer(arguments, "hostWorkspace", "");
143-
_debuggee.RemoteWorkspace = GetFromContainer(arguments, "remoteWorkspace", "");
144156

145157
DebugClientFactory debugClientFactory;
146158
try

src/VSCode.DebugAdapter/PathHandlingStrategy.cs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,34 @@ public string ConvertDebuggerPathToClient(string path)
6363
}
6464
}
6565

66-
public string ConvertClientPathToDebugger(string clientPath)
67-
{
68-
if (clientPath == null) {
69-
return null;
70-
}
66+
public string ConvertClientPathToDebugger(string clientPath)
67+
{
68+
if (clientPath == null) {
69+
return null;
70+
}
7171

72-
if (DebuggerPathsAreUri) {
73-
if (ClientPathsAreUri) {
74-
return clientPath;
75-
}
76-
else {
77-
var uri = new System.Uri(clientPath);
78-
return uri.AbsoluteUri;
79-
}
80-
}
81-
else {
82-
if (ClientPathsAreUri) {
83-
if (Uri.IsWellFormedUriString(clientPath, UriKind.Absolute)) {
84-
Uri uri = new Uri(clientPath);
85-
return uri.LocalPath;
86-
}
87-
Console.Error.WriteLine("path not well formed: '{0}'", clientPath);
88-
return null;
89-
}
90-
else {
91-
return clientPath;
92-
}
93-
}
94-
}
95-
}
72+
if (DebuggerPathsAreUri) {
73+
if (ClientPathsAreUri) {
74+
return clientPath;
75+
}
76+
else {
77+
var uri = new System.Uri(clientPath);
78+
return uri.AbsoluteUri;
79+
}
80+
}
81+
else {
82+
if (ClientPathsAreUri) {
83+
if (Uri.IsWellFormedUriString(clientPath, UriKind.Absolute)) {
84+
Uri uri = new Uri(clientPath);
85+
return uri.LocalPath;
86+
}
87+
Console.Error.WriteLine("path not well formed: '{0}'", clientPath);
88+
return null;
89+
}
90+
else {
91+
return clientPath;
92+
}
93+
}
94+
}
95+
}
9696
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
using System;
8+
using System.Runtime.InteropServices;
9+
10+
namespace VSCode.DebugAdapter
11+
{
12+
public class WorkspaceMapper
13+
{
14+
15+
private Workspace _localWorkspace;
16+
private Workspace _remoteWorkspace;
17+
18+
public WorkspaceMapper(string localPath, string remotePath) {
19+
20+
this._localWorkspace = new Workspace(localPath);
21+
this._remoteWorkspace = new Workspace(remotePath);
22+
}
23+
24+
public string LocalToRemote(string path)
25+
{
26+
return ConvertPath(path, _localWorkspace, _remoteWorkspace);
27+
}
28+
29+
public string RemoteToLocal(string path)
30+
{
31+
return ConvertPath(path, _remoteWorkspace, _localWorkspace);
32+
}
33+
34+
private string ConvertPath(string path, Workspace fromPrefix, Workspace toPrefix)
35+
{
36+
if (string.IsNullOrWhiteSpace(path) ||
37+
string.IsNullOrWhiteSpace(fromPrefix.Original) ||
38+
string.IsNullOrWhiteSpace(toPrefix.Original))
39+
return path;
40+
41+
var normalizedPath = path.Replace('\\', '/');
42+
var normalizedFrom = fromPrefix.Normalized.TrimEnd('/');
43+
44+
var comparison = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
45+
? StringComparison.OrdinalIgnoreCase
46+
: StringComparison.Ordinal;
47+
48+
if (normalizedPath.StartsWith(toPrefix.Normalized, comparison) ||
49+
!normalizedPath.StartsWith(normalizedFrom, comparison))
50+
return path;
51+
52+
var relativePath = normalizedPath.Substring(normalizedFrom.Length).TrimStart('/');
53+
54+
var result = toPrefix.Original.TrimEnd('/', '\\') + "/" + relativePath;
55+
56+
return result;
57+
}
58+
59+
}
60+
61+
internal class Workspace
62+
{
63+
public string Original;
64+
public string Normalized;
65+
66+
internal Workspace(string path)
67+
{
68+
this.Original = path;
69+
this.Normalized = path.Replace('\\', '/');
70+
}
71+
72+
}
73+
74+
}

src/VSCode.DebugAdapter/package.json

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,22 @@
133133
"description": "Кодировка вывода отлаживаемого приложения. Отладчик будет интерпретировать вывод приложения в указанной кодировке",
134134
"default": ""
135135
},
136-
"hostWorkspace": {
137-
"type": "string",
138-
"description": "Путь к каталогу проекта на локальной машине (при отладке удаленного процесса)",
139-
"default": ""
140-
},
141-
"remoteWorkspace": {
142-
"type": "string",
143-
"description": "Путь к каталогу проекта на удаленной машине (при отладке удаленного процесса)",
144-
"default": ""
136+
"pathsMapper": {
137+
"type": "object",
138+
"description": "Сопоставление путей между локальной и удаленной машиной (для удаленной отладки)",
139+
"properties": {
140+
"localPath": {
141+
"type": "string",
142+
"description": "Путь к каталогу проекта на локальной машине.",
143+
"default": ""
144+
},
145+
"remotePath": {
146+
"type": "string",
147+
"description": "Путь к каталогу проекта на удаленной машине.",
148+
"default": ""
149+
}
150+
},
151+
"required": ["localPath", "remotePath"]
145152
}
146153

147154
}

0 commit comments

Comments
 (0)