Skip to content

Commit

Permalink
10011: Minor code improvements
Browse files Browse the repository at this point in the history
- Improve test description to be consistent with other tests
- Move the SCHEME and SCHEME_PATTERN constants to debug/common for
reusability
  • Loading branch information
CamilleLetavernier committed Jun 3, 2022
1 parent b281243 commit 56aef99
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 70 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## v1.27.0 - Unreleased

- [plugin] moved `WebviewViewResolveContext` from `window` to `root` namespace [#11216](https://github.com/eclipse-theia/theia/pull/11216) - Contributed on behalf of STMicroelectronics
- [plugin] added support for `DebugProtocolBreakpoint` and `DebugProtocolSource` [#10011](https://github.com/eclipse-theia/theia/issues/10011) - Contributed on behalf of STMicroelectronics

<a name="breaking_changes_1.27.0">[Breaking Changes:](#breaking_changes_1.27.0)</a>

Expand Down Expand Up @@ -220,7 +221,6 @@
- [vsx-registry] updated `requestretry` from `v3.1.0` to `v7.0.0` [#10831](https://github.com/eclipse-theia/theia/pull/10831)
- [workspace] fixed `'save as'` for `untitled` schemes [#10608](https://github.com/eclipse-theia/theia/pull/10608)
- [workspace] fixed the styling of the `path` in the dialog [#10814](https://github.com/eclipse-theia/theia/pull/10814)
- [plugin] added support for `DebugProtocolBreakpoint` and `DebugProtocolSource` [#10011](https://github.com/eclipse-theia/theia/issues/10011) - Contributed on behalf of STMicroelectronics

<a name="breaking_changes_1.24.0">[Breaking Changes:](#breaking_changes_1.24.0)</a>

Expand Down
10 changes: 5 additions & 5 deletions packages/debug/src/browser/model/debug-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import URI from '@theia/core/lib/common/uri';
import { DebugProtocol } from 'vscode-debugprotocol/lib/debugProtocol';
import { DebugSession } from '../debug-session';
import { URI as Uri } from '@theia/core/shared/vscode-uri';
import { DEBUG_SCHEME, SCHEME_PATTERN } from '../../common/debug-uri-utils';

export class DebugSourceData {
readonly raw: DebugProtocol.Source;
Expand Down Expand Up @@ -58,7 +59,7 @@ export class DebugSource extends DebugSourceData {
}

get inMemory(): boolean {
return this.uri.scheme === DebugSource.SCHEME;
return this.uri.scheme === DEBUG_SCHEME;
}

get name(): string {
Expand All @@ -75,16 +76,15 @@ export class DebugSource extends DebugSourceData {
return this.labelProvider.getLongName(this.uri);
}

static SCHEME = 'debug';
static SCHEME_PATTERN = /^[a-zA-Z][a-zA-Z0-9\+\-\.]+:/;
static SCHEME = DEBUG_SCHEME;
static toUri(raw: DebugProtocol.Source): URI {
if (raw.sourceReference && raw.sourceReference > 0) {
return new URI().withScheme(DebugSource.SCHEME).withPath(raw.name!).withQuery(String(raw.sourceReference));
return new URI().withScheme(DEBUG_SCHEME).withPath(raw.name!).withQuery(String(raw.sourceReference));
}
if (!raw.path) {
throw new Error('Unrecognized source type: ' + JSON.stringify(raw));
}
if (raw.path.match(DebugSource.SCHEME_PATTERN)) {
if (raw.path.match(SCHEME_PATTERN)) {
return new URI(raw.path);
}
return new URI(Uri.file(raw.path));
Expand Down
24 changes: 24 additions & 0 deletions packages/debug/src/common/debug-uri-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/********************************************************************************
* Copyright (C) 2022 STMicroelectronics and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

/**
* The URI scheme for debug URIs.
*/
export const DEBUG_SCHEME = 'debug';
/**
* The pattern for URI schemes.
*/
export const SCHEME_PATTERN = /^[a-zA-Z][a-zA-Z0-9\+\-\.]+:/;
121 changes: 61 additions & 60 deletions packages/plugin-ext/src/plugin/node/debug/debug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { DebugSession } from '@theia/plugin';
import * as chai from 'chai';
import { ProxyIdentifier, RPCProtocol } from '../../../common/rpc-protocol';
Expand All @@ -22,72 +21,74 @@ import { DebugExtImpl } from './debug';

const expect = chai.expect;

describe('Debug source URI:', () => {

const mockRPCProtocol: RPCProtocol = {
getProxy<T>(_proxyId: ProxyIdentifier<T>): T {
return {} as T;
},
set<T, R extends T>(_id: ProxyIdentifier<T>, instance: R): R {
return instance;
},
dispose(): void {
// Nothing
}
};
describe('Debug API', () => {

const debug = new DebugExtImpl(mockRPCProtocol);
describe('#asDebugSourceURI', () => {

it('Should use sourceReference, path and sessionId', () => {
const source = {
sourceReference: 3,
path: 'test/path'
const mockRPCProtocol: RPCProtocol = {
getProxy<T>(_proxyId: ProxyIdentifier<T>): T {
return {} as T;
},
set<T, R extends T>(_id: ProxyIdentifier<T>, instance: R): R {
return instance;
},
dispose(): void {
// Nothing
}
};
const session = { id: 'test-session' } as DebugSession;
const uri = debug.asDebugSourceUri(source, session);
expect(uri.toString(true)).to.be.equal('debug:test/path?ref=3&session=test-session');
});

it('should use sourceReference', () => {
const source = {
sourceReference: 5
};
const uri = debug.asDebugSourceUri(source);
expect(uri.toString(true)).to.be.equal('debug:?ref=5');
});
const debug = new DebugExtImpl(mockRPCProtocol);

it('should use sourceReference and session', () => {
const source = {
sourceReference: 5
};
const session = { id: 'test-session' } as DebugSession;
const uri = debug.asDebugSourceUri(source, session);
expect(uri.toString(true)).to.be.equal('debug:?ref=5&session=test-session');
});
it('should use sourceReference, path and sessionId', () => {
const source = {
sourceReference: 3,
path: 'test/path'
};
const session = { id: 'test-session' } as DebugSession;
const uri = debug.asDebugSourceUri(source, session);
expect(uri.toString(true)).to.be.equal('debug:test/path?ref=3&session=test-session');
});

it('should use sourceReference and path', () => {
const source = {
sourceReference: 4,
path: 'test/path'
};
const uri = debug.asDebugSourceUri(source);
expect(uri.toString(true)).to.be.equal('debug:test/path?ref=4');
});
it('should use sourceReference', () => {
const source = {
sourceReference: 5
};
const uri = debug.asDebugSourceUri(source);
expect(uri.toString(true)).to.be.equal('debug:?ref=5');
});

it('should use path', () => {
const source = {
path: 'scheme:/full/path'
};
const uri = debug.asDebugSourceUri(source);
expect(uri.toString(true)).to.be.equal('scheme:/full/path');
});
it('should use sourceReference and session', () => {
const source = {
sourceReference: 5
};
const session = { id: 'test-session' } as DebugSession;
const uri = debug.asDebugSourceUri(source, session);
expect(uri.toString(true)).to.be.equal('debug:?ref=5&session=test-session');
});

it('should use file path', () => {
const source = {
path: '/full/path'
};
const uri = debug.asDebugSourceUri(source);
expect(uri.toString(true)).to.be.equal('file:///full/path');
});
it('should use sourceReference and path', () => {
const source = {
sourceReference: 4,
path: 'test/path'
};
const uri = debug.asDebugSourceUri(source);
expect(uri.toString(true)).to.be.equal('debug:test/path?ref=4');
});

it('should use path', () => {
const source = {
path: 'scheme:/full/path'
};
const uri = debug.asDebugSourceUri(source);
expect(uri.toString(true)).to.be.equal('scheme:/full/path');
});

it('should use file path', () => {
const source = {
path: '/full/path'
};
const uri = debug.asDebugSourceUri(source);
expect(uri.toString(true)).to.be.equal('file:///full/path');
});
});
});
7 changes: 3 additions & 4 deletions packages/plugin-ext/src/plugin/node/debug/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { PluginPackageDebuggersContribution } from '../../../common/plugin-proto
import { RPCProtocol } from '../../../common/rpc-protocol';
import { CommandRegistryImpl } from '../../command-registry';
import { ConnectionImpl } from '../../../common/connection';
import { DEBUG_SCHEME, SCHEME_PATTERN } from '@theia/debug/lib/common/debug-uri-utils';
import {
Disposable, Breakpoint as BreakpointExt, SourceBreakpoint, FunctionBreakpoint, Location, Range,
DebugAdapterServer, DebugAdapterExecutable, DebugAdapterNamedPipeServer, DebugAdapterInlineImplementation,
Expand Down Expand Up @@ -187,8 +188,6 @@ export class DebugExtImpl implements DebugExt {
return Disposable.create(() => this.descriptorFactories.delete(debugType));
}

static SCHEME = 'debug';
static SCHEME_PATTERN = /^[a-zA-Z][a-zA-Z0-9\+\-\.]+:/;
asDebugSourceUri(source: theia.DebugProtocolSource, session?: theia.DebugSession): theia.Uri {
const raw = source as DebugProtocol.Source;
const uri = this.getDebugSourceUri(raw, session?.id);
Expand All @@ -201,12 +200,12 @@ export class DebugExtImpl implements DebugExt {
if (sessionId) {
query += `&session=${sessionId}`;
}
return new TheiaURI().withScheme(DebugExtImpl.SCHEME).withPath(raw.path || '').withQuery(query);
return new TheiaURI().withScheme(DEBUG_SCHEME).withPath(raw.path || '').withQuery(query);
}
if (!raw.path) {
throw new Error('Unrecognized source type: ' + JSON.stringify(raw));
}
if (raw.path.match(DebugExtImpl.SCHEME_PATTERN)) {
if (raw.path.match(SCHEME_PATTERN)) {
return new TheiaURI(raw.path);
}
return new TheiaURI(URI.file(raw.path));
Expand Down

0 comments on commit 56aef99

Please sign in to comment.