Skip to content

Commit

Permalink
Massive rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
rafed committed Oct 11, 2024
1 parent a066668 commit bea4bcc
Show file tree
Hide file tree
Showing 35 changed files with 884 additions and 884 deletions.
108 changes: 54 additions & 54 deletions codemetrica/DetectLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
import { Language } from "./language";
import vscodeLangDetection from '@vscode/vscode-languagedetection';
const { ModelOperations } = vscodeLangDetection;

export class DetectLanguage {
private static extensionToLanguageMap = new Map<string, Language>([
['c', Language.C],
['cpp', Language.CPP],
['cs', Language.CSharp],
['go', Language.GO],
['java', Language.JAVA],
['js', Language.JAVASCRIPT],
['php', Language.PHP],
['py', Language.PYTHON],
['rb', Language.RUBY],
['ts', Language.TYPESCRIPT]
]);

static fromExtension(extension: string): Language {
const language = this.extensionToLanguageMap.get(extension.toLowerCase());
if (!language) {
throw new Error(`Unsupported language extension: ${extension}`);
}
return language;
}

static async fromSourceCode(sourceCode: string): Promise<Language> {
const modelOperations = new ModelOperations();

try {
const result = await modelOperations.runModel(sourceCode);

const mostConfidentLanguage = result.reduce((prev, current) => {
return (prev.confidence > current.confidence) ? prev : current;
});

return this.fromExtension(mostConfidentLanguage.languageId);

} catch (error) {
console.error('Error running detector model:', error);
throw new Error('Failed to detect language'); // Optionally throw an error if needed
}
}

static fromAntlrContext(ctx: any): Language {
const constructor_name = ctx.constructor.name.toLowerCase();
if (constructor_name.includes('python')) {
return Language.PYTHON;
} else if (constructor_name.includes('java')) {
return Language.JAVA;
}

throw new Error(`Unsupported language: ${constructor_name}`);
}
import { Language } from "./language";
import vscodeLangDetection from '@vscode/vscode-languagedetection';
const { ModelOperations } = vscodeLangDetection;

export class DetectLanguage {
private static extensionToLanguageMap = new Map<string, Language>([
['c', Language.C],
['cpp', Language.CPP],
['cs', Language.CSharp],
['go', Language.GO],
['java', Language.JAVA],
['js', Language.JAVASCRIPT],
['php', Language.PHP],
['py', Language.PYTHON],
['rb', Language.RUBY],
['ts', Language.TYPESCRIPT]
]);

static fromExtension(extension: string): Language {
const language = this.extensionToLanguageMap.get(extension.toLowerCase());
if (!language) {
throw new Error(`Unsupported language extension: ${extension}`);
}
return language;
}

static async fromSourceCode(sourceCode: string): Promise<Language> {
const modelOperations = new ModelOperations();

try {
const result = await modelOperations.runModel(sourceCode);

const mostConfidentLanguage = result.reduce((prev, current) => {
return (prev.confidence > current.confidence) ? prev : current;
});

return this.fromExtension(mostConfidentLanguage.languageId);

} catch (error) {
console.error('Error running detector model:', error);
throw new Error('Failed to detect language'); // Optionally throw an error if needed
}
}

static fromAntlrContext(ctx: any): Language {
const constructor_name = ctx.constructor.name.toLowerCase();
if (constructor_name.includes('python')) {
return Language.PYTHON;
} else if (constructor_name.includes('java')) {
return Language.JAVA;
}

throw new Error(`Unsupported language: ${constructor_name}`);
}
}
24 changes: 12 additions & 12 deletions codemetrica/Language.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export enum Language {
C,
CPP,
CSharp,
GO,
JAVA,
JAVASCRIPT,
PYTHON,
PHP,
RUBY,
TYPESCRIPT,
UNKNOWN
export enum Language {
C,
CPP,
CSharp,
GO,
JAVA,
JAVASCRIPT,
PYTHON,
PHP,
RUBY,
TYPESCRIPT,
UNKNOWN
}
62 changes: 31 additions & 31 deletions codemetrica/SourceParser.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { ICodeBlock } from "./interface/ICodeBlock";

import { parsePythonSource } from './language/python/parser';
import { parseJavaSource } from './language/java/parser';

import * as fs from 'fs';
import { JavaCodeBlock } from './language/java';
import { PyCodeBlock } from './language/python';

export class ParseSource {
static fromFile(filePath: string): ICodeBlock {
const extension = filePath.split('.').pop();

if(!extension) {
throw new Error('No valid file extension');
}

const text = fs.readFileSync(filePath, 'utf-8');
return ParseSource.fromText(text, extension);
}

static fromText(text: string, extension: string): JavaCodeBlock | PyCodeBlock {
switch(extension) {
case 'java':
return new JavaCodeBlock(parseJavaSource(text));
case 'py':
return new PyCodeBlock(parsePythonSource(text));
default:
throw new Error('Unsupported language');
}
}
import { ICodeBlock } from "./interface/ICodeBlock";

import { parsePythonSource } from './language/python/parser';
import { parseJavaSource } from './language/java/parser';

import * as fs from 'fs';
import { JavaCodeBlock } from './language/java';
import { PyCodeBlock } from './language/python';

export class ParseSource {
static fromFile(filePath: string): ICodeBlock {
const extension = filePath.split('.').pop();

if(!extension) {
throw new Error('No valid file extension');
}

const text = fs.readFileSync(filePath, 'utf-8');
return ParseSource.fromText(text, extension);
}

static fromText(text: string, extension: string): JavaCodeBlock | PyCodeBlock {
switch(extension) {
case 'java':
return new JavaCodeBlock(parseJavaSource(text));
case 'py':
return new PyCodeBlock(parsePythonSource(text));
default:
throw new Error('Unsupported language');
}
}
}
12 changes: 6 additions & 6 deletions codemetrica/interface/IClass.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ICodeBlock } from './ICodeBlock';
import { IMethod } from './IMethod';

export interface IClass extends ICodeBlock {
name: string;
getMethods(): IMethod[];
import { ICodeBlock } from './ICodeBlock';
import { IMethod } from './IMethod';

export interface IClass extends ICodeBlock {
name: string;
getMethods(): IMethod[];
}
10 changes: 5 additions & 5 deletions codemetrica/interface/ICodeBlock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Language } from "../language";

export abstract class ICodeBlock {
ctx: any;
language?: Language;
import { Language } from "../language";

export abstract class ICodeBlock {
ctx: any;
language?: Language;
}
12 changes: 6 additions & 6 deletions codemetrica/interface/IFunction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ICodeBlock } from './ICodeBlock';
import { IParameter } from './IParameter';

export interface IFunction extends ICodeBlock{
name: string;
getParameters(): IParameter[];
import { ICodeBlock } from './ICodeBlock';
import { IParameter } from './IParameter';

export interface IFunction extends ICodeBlock{
name: string;
getParameters(): IParameter[];
}
6 changes: 3 additions & 3 deletions codemetrica/interface/IMethod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IParameter } from './IParameter';
import { IFunction } from './IFunction';

import { IParameter } from './IParameter';
import { IFunction } from './IFunction';

export interface IMethod extends IFunction {}
8 changes: 4 additions & 4 deletions codemetrica/interface/IParameter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export interface IParameter {
name: string;
type: string | null;

export interface IParameter {
name: string;
type: string | null;
}
10 changes: 5 additions & 5 deletions codemetrica/interface/metric/IMethodLength.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IFunction } from '../IFunction';
import { IMethod } from '../IMethod';

export interface IMethodLength {
calculate(method: IMethod | IFunction): number;
import { IFunction } from '../IFunction';
import { IMethod } from '../IMethod';

export interface IMethodLength {
calculate(method: IMethod | IFunction): number;
}
10 changes: 5 additions & 5 deletions codemetrica/interface/smell/ILongMethod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IFunction } from '../IFunction';
import { IMethod } from '../IMethod';

export interface ILongMethod {
detect(method: IFunction | IMethod): boolean;
import { IFunction } from '../IFunction';
import { IMethod } from '../IMethod';

export interface ILongMethod {
detect(method: IFunction | IMethod): boolean;
}
62 changes: 31 additions & 31 deletions codemetrica/language/java/JavaClass.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { JavaCodeBlockBase } from "./JavaCodeBlockBase";
import { JavaMethod } from "./JavaMethod";
import { NormalClassDeclarationContext, MethodDeclarationContext } from "../../../grammars-v4/java/java20/Java20Parser";
import JavaParserVisitor from "../../../grammars-v4/java/java20/Java20ParserVisitor";

export class JavaClass extends JavaCodeBlockBase<NormalClassDeclarationContext> {
constructor(ctx: NormalClassDeclarationContext) {
super(ctx);
}

get name(): string {
return this.ctx.typeIdentifier().getText();
}

getMethods(): JavaMethod[] {
class MethodVisitor extends JavaParserVisitor<void> {
methods: JavaMethod[];
constructor() {
super();
this.methods = [];
}

visitMethodDeclaration = (ctx: MethodDeclarationContext) => {
this.methods.push(new JavaMethod(ctx));
}
}

const visitor = new MethodVisitor();
visitor.visit(this.ctx);
return visitor.methods;
}
import { JavaCodeBlockBase } from "./JavaCodeBlockBase";
import { JavaMethod } from "./JavaMethod";
import { NormalClassDeclarationContext, MethodDeclarationContext } from "../../../grammars-v4/java/java20/Java20Parser";
import JavaParserVisitor from "../../../grammars-v4/java/java20/Java20ParserVisitor";

export class JavaClass extends JavaCodeBlockBase<NormalClassDeclarationContext> {
constructor(ctx: NormalClassDeclarationContext) {
super(ctx);
}

get name(): string {
return this.ctx.typeIdentifier().getText();
}

getMethods(): JavaMethod[] {
class MethodVisitor extends JavaParserVisitor<void> {
methods: JavaMethod[];
constructor() {
super();
this.methods = [];
}

visitMethodDeclaration = (ctx: MethodDeclarationContext) => {
this.methods.push(new JavaMethod(ctx));
}
}

const visitor = new MethodVisitor();
visitor.visit(this.ctx);
return visitor.methods;
}
}
Loading

0 comments on commit bea4bcc

Please sign in to comment.